数组
1.判断数组内是否有重复元素
/**判断String数组内是否有重复元素*/
public static boolean checkRepeat(String[] strings){
boolean result = false;
List<String> list = new ArrayList<>();
for (int i=0; i < strings.length; i++){
if (!list.contains(strings[i])){
list.add(strings[i]);
}
}
if (list.size() < strings.length){
result = true;
return result;
}else {
return result;
}
}
测试:
结果:
2.数组转为List集合
(1)Collections.addAll()方法
System.out.println("----------数组转为List集合:Collections.addAll()方法-----------------------------------");
String[] strings = {"aa","bb","cc"};
List<String> list3 = new ArrayList<>();
Collections.addAll(list3, strings);
System.out.println(list3.toString());
测试:
(2)循环添加数组元素的方法
System.out.println("----------数组转为List集合:循环添加数组元素的方法-----------------------------------");
String[] string4 = {"aa","bb","cc","dd"};
List<String> list4 = new ArrayList<>();
for (int i=0; i<string4.length; i++){
list4.add(string4[i]);
}
System.out.println(list4.toString());
测试:
(3)Arrays.asList()方法
返回一个受指定数组支持的固定大小的列表,不能做Add、Remove等操作
System.out.println("----------数组转为List集合:Arrays.asList()方法-----------------------------------");
String[] string5 = {"aa","bb","cc","dd","ee"};
List<String> list5 = new ArrayList<>();
list5 = Arrays.asList(string5);
System.out.println(list5.toString());
测试:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。