数组元素的排序
int array[]= {-1,3,0,5,2};
Arrays.sort(array);
数组元素的位置查找
int array[]= {-1,3,0,5,2};
System.out.println(Arrays.binarySearch(array,8));
for(int a:array) {
System.out.println(a);
}
数组元素的反转
List<String> list=new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
Iterator<String> iterator=list.iterator();
System.out.println("迭代前的数组");
while(iterator.hasNext()) {
System.out.print(iterator.next());
}
System.out.println();
System.out.println("迭代之后的数组");
Collections.reverse(list);
for(String i:list) {
System.out.print(i);
}
数组中的最大值和最小值
Integer[] integer= {1,5,-10,8,-12};
int min=(int) Collections.min(Arrays.asList(integer));
int max=(int) Collections.max(Arrays.asList(integer));
System.out.println(min);
System.out.println(max);
数组合并
String st[]= {"A","B"};
String sm[]= {"C","D"};
System.out.println(st);
@SuppressWarnings("rawtypes")
List list=new ArrayList(Arrays.asList(st));
System.out.println("list:"+list);
System.out.println(Arrays.asList(sm));
list.addAll(Arrays.asList(sm));
System.out.println(list);
System.out.println("转换成数组对象");
Object[] c=list.toArray();
System.out.println(c.length);
System.out.println(c[0]);
for(Object string:c) {
System.out.println(string);
}
System.out.println(Arrays.toString(c));
数组填充
int data[]= {1,23,10,58};
for(int child:data) {
System.out.println(child);
}
System.out.println();
Arrays.fill(data,-78);
Arrays.fill(data,0,2,-78);
Integer[] integers= {12,34,89};
数组扩容
String str1[]=new String[5];
str1[0]="A";
str1[1]="B";
str1[2]="C";
String str1[]= {"A","B","C"};
String str2[]= {"D","E"};
System.arraycopy(str2, 0, str1, 3, 2);
删除数组元素
ArrayList<String> strings=new ArrayList<String>();
strings.add(0, "第first元素");
strings.add(1, "第second元素");
strings.add(2, "第third元素");
strings.remove(1);
strings.remove("第third元素");
数组差值
ArrayList list1=new ArrayList();
ArrayList list2=new ArrayList();
list1.add("A");
list1.add("B");
list1.add("C");
list2.add("B");
list2.add("C");
list1.removeAll(list2);
数组交集
ArrayList list1=new ArrayList();
ArrayList list2=new ArrayList();
list1.add("A");
list1.add("B");
list1.add("C");
list2.add("B");
list1.retainAll(list2);
数组元素查找
ArrayList list1=new ArrayList();
ArrayList list2=new ArrayList();
list1.add("A");
list1.add("B");
list1.add("C");
list2.add("B");
System.out.println(list1.contains("A"));
System.out.println(list1.contains("D"));
判断数组
int[] int1= {1,2};
int[] int2= {3,4};
int[] int3= {1,2};
System.out.println(Arrays.equals(int1, int2));
System.out.println(Arrays.equals(int1, int3));
数组合并
String strs1[]= {"A","B","D","P"};
String strs2[]= {"B","C","D","F"};
String strs3[]= new String[3];
ArrayList lis1=new ArrayList(Arrays.asList(strs1));
ArrayList lis2=new ArrayList(Arrays.asList(strs2));
System.out.println(lis1.get(0));
System.out.println(lis1.size());
ArrayList returnList=unionList(lis1, lis2);
System.out.println(returnList);
Object newstr[]=returnList.toArray();
System.out.println();
public static ArrayList unionList(ArrayList list1,ArrayList list2) {
ArrayList list = new ArrayList();
for(Object lists1:list1) {
list.add(lists1);
}
for(Object lists2:list2) {
list.add(lists2);
}
for(int i=0;i<list.size();i++) {
for(int j=0;j<list.size();j++) {
if(i!=j && list.get(i)==list.get(j)) {
System.out.println("重复原始为:"+list.get(i));
list.remove(i);
}
}
}
return list;
}