java 数组学习

遍历数组 --- for和foreach

1 int[][] A = {{2,4},{3,5}};
2 int i = 0;
3 for (int[] is : A) {
4     i++;
5     int j = 0;
6     for (int jl : is) {
7         System.out.print(jl + " ");
8     }
9 }//输出2 4 3 5

填充替换数组元素 --- fill()

1 int[] A = {2,4,3,5};
2 Arrays.fill(A, 4);//输出4 4 4 4 ,替换数组所有元素
3 Arrays.fill(A, 0, 2, 9);//输出9 9 3 5,替换指定索引元素

数组排序 --- sort()

1 int[] A = {2,4,3,5};
2 Arrays.sort(A);//2 3 4 5
3 
4 int[] A = {5,2,4,3,5};
5 Arrays.sort(A, 0, 3);//2 4 5 3 5

复制数组 --- copyOf()、 copyOfRange()

1 int[] A = {5,2,4,3,5};
2 int[] B = Arrays.copyOf(A, 3);//5 2 4
3 int[] B = Arrays.copyOf(A, 7);//5 2 4 3 5 0 0
4 int[] B = Arrays.copyOfRange(A, 2, 5);//4 3 5
5 int[] B = Arrays.copyOfRange(A, 2, 7);//4 3 5 0 0

数组查询 --- Arrays.binarySearch([], key);

猜你喜欢

转载自www.cnblogs.com/Johnny-yu/p/11304304.html
0条评论
添加一条新回复
  
今日推荐