ArrayList数组去重
//创建动态数组
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(3);
list.add(4);
list.add(6);
list.add(1);
System.out.println(list);//[1, 2, 3, 4, 5, 3, 4, 6, 1]
//去重
//创建HashSet数组
Set<Integer> set = new HashSet<Integer>(list);
System.out.print(set);//[1, 2, 3, 4, 5, 6]
System.out.println();
//重新转化为ArrayList数组
List<Integer> list1 = new ArrayList<Integer>(set);
System.out.print(list1);//[1, 2, 3, 4, 5, 6]
利用HashSet无序数组无重复的特点,将有序数组放入无序数组中“清洗”一下,重新转化为有序数组去重
应用数据类型打印
//当打印内容的为对象时,后台显示为对象在内存的地址
Say say;
System.out.println(say);
com.dodoke.d0829.Say@7382f612
com.dodoke.d0829.Say@1055e4af
com.dodoke.d0829.Say@3caeaf62
com.dodoke.d0829.Say@e6ea0c6
com.dodoke.d0829.Say@6a38e57f
com.dodoke.d0829.Say@5577140b
com.dodoke.d0829.Say@1c6b6478
com.dodoke.d0829.Say@67f89fa3
此时需要重写对象对应类toString方法
public class Say {
String name;
public String toString() {
return name;
}
}
此时打印台便会打印出对象的内容。