目录:
- 声明数组;
- 初始化数组;
- 查看数组长度;
- 遍历数组;
- int数组转成string数组;
- 从array中创建arraylist;
- 数组中是否包含某一个值;
- 将数组转成set集合;
- 将数组转成list集合;
- Arrays.fill()填充数组;
- 数组排序;
- 复制数组;
- 比较两个数组;
- 去重复;
- 查询数组中的最大值和最小值;
备注:文内代码具有关联性。
1.声明数组;
1 String [] arr; 2 int arr1[]; 3 String[] array=new String[5]; 4 int score[]=new int[3];
2.初始化数组;
1 //静态初始化 2 int arr2[]=new int[]{1,2,3,4,5}; 3 String[] array1={"马超","马云","关羽","刘备","张飞"}; 4 String[] array2=new String[]{"黄渤","张艺兴","孙红雷","小猪","牙哥","黄磊"}; 5 int score[]=new int[3]; 6 //动态初始化 7 for(int i=0;i<score.length;i++) 8 { 9 score[i]=i+1; 10 }
3.查看数组长度;
1 int length=array1.length; 2 System.out.println("length: "+array1.length);
4.遍历数组;
1 for (int i = 0; i < array1.length; i++) { 2 System.out.println(array1[i]); 3 }
5.int数组转成string数组;
1 int[] array3={1,2,3,4,5,6,7,8,9,0}; 2 String arrStrings=Arrays.toString(array3); 3 System.out.println(arrStrings);
6.从array中创建arraylist;
1 ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(array1)); 2 System.out.println(arrayList);
7.数组中是否包含某一个值;
1 String a="马超"; 2 String[] array1={"马超","马云","关羽","刘备","张飞"}; 3 if (Arrays.asList(array1).contains(a)) { 4 System.out.println("马超在这里"); 5 }
8.将数组转成set集合;
1 String[] array2=new String[]{"黄渤","张艺兴","孙红雷","小猪","牙哥","黄磊"}; 2 Set<String> set=new HashSet<String>(Arrays.asList(array2)); 3 System.out.println(set);
9.将数组转成list集合;
1 String[] array2=new String[]{"黄渤","张艺兴","孙红雷","小猪","牙哥","黄磊"}; 2 //方法 1. 3 List<String> list=new ArrayList<String>(); 4 for (int i = 0; i < array2.length; i++) { 5 list.add(array2[i]); 6 } 7 8 String[] arrStrings2={"1","2","3"}; 9 //方法 2. 10 List<String > list2=java.util.Arrays.asList(arrStrings2); 11 System.out.println(list2);
10.Arrays.fill()填充数组
1 int[] arr3=new int[5]; 2 Arrays.fill(arr3, 10); //将数组全部填充10 3 //遍历输出 4 for (int i = 0; i < arr3.length; i++) { 5 System.out.println(arr3[i]); 6 }
11.数组排序;
1 //方法 1. 2 int[] arr4 = {3, 7, 2, 1, 9}; 3 Arrays.sort(arr4); //.sort(int[] a) 放入数组名字 4 for (int i = 0; i < arr4.length; i++) { 5 System.out.println(arr4[i]); 6 } 7 //方法 2. 8 int[] arr5 = {3, 7, 2, 1, 9,3,45,7,8,8,3,2,65,34,5}; 9 Arrays.sort(arr5, 1, 4); //.sort(a, fromIndex, toIndex) 从第几个到第几个之间的进行排序 10 for (int i = 0; i < arr5.length; i++) { 11 System.out.println(arr5[i]); 12 }
12.复制数组;
1 //方法 1. 2 int[] arr6 = {3, 7, 2, 1}; 3 int[] arr7=Arrays.copyOf(arr6, 10); //指定新数组的长度 4 //方法 2. 5 int[] arr8=Arrays.copyOfRange(arr6, 1, 3); //只复制从索引[1]到索引[3]之间的元素(不包括索引[3]的元素) 6 for (int i = 0; i < arr8.length; i++) { 7 System.out.println(arr8[i]); 8 }
13.比较两个数组;
1 int[] arr9 = {1, 2, 3, 4,5,6,7,8,9,0}; 2 boolean arr10=Arrays.equals(arr6, arr9); 3 System.out.println(arr10);
14.去重复;
1 int[] arr11 = {1, 2, 3, 4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3}; 2 //利用set的特性 3 Set<Integer> set2=new HashSet<Integer>(); 4 for (int i = 0; i < arr11.length; i++) { 5 set2.add(arr11[i]); 6 } 7 System.out.println(set2); 8 int[] arr12 = new int[set2.size()]; 9 int j=0; 10 for (Integer i:set2) { 11 arr12[j++]=i; 12 } 13 System.out.println(Arrays.toString(arr12));
15.查询数组中的最大值和最小值;
1 int[] arr11 = {10, 2, 3, 4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3}; 2 //计算最大值 3 int max = arr11[0]; 4 for (int i = 1; i < arr11.length; i++) { 5 if (arr11[i] > max) { 6 max = arr11[i]; 7 } 8 } 9 System.out.println("Max is " + max); 10 //计算最小值 11 int min = arr11[0]; 12 for (int i = 0; i < arr11.length; i++) { 13 if (arr11[i]<min) { 14 min = arr11[i]; 15 } 16 } 17 System.out.println("Min is " + min);