import java.util.Arrays;
public class Two_dimensional_array {
public static void main(String[] args) {
//二维数组的定义
int twoDimArray[][] = new int[3][5];
int[][] twoDimArray1 = {{1,3}, {22,56,47}, {0}};
int[][] twoDimArray2 = new int[][]{{1,3}, {22,56,47}, {0}};
double[][] dwa ;
dwa = new double[][]{{1.0,3.1}, {22.4,56.7,47.8}, {0.0}};
int[][] twoDimArray3;
twoDimArray3 = new int[][]{{1,3}, {22,56,47}, {0}};
//二维数组的下标访问
int[][] test_dArray_index = {{1,3}, {22,56,47}, {0}};
// test_dArray_index[1] == {22,56,47};
// test_dArray_index[1][0] == 22; ==> {22,56,47}[0];
//二维数组的遍历
int[][] test_twoDimArray1 = {{1,3,7}, {22,56,47,4}, {0,1}};
for(int i=0; i<test_twoDimArray1.length; i++){
for(int j=0; j<test_twoDimArray1[i].length; j++){
System.out.print(test_twoDimArray1[i][j] + " ");
}
}
System.out.println();
//二维数组使用常用的一些操作数组的方法
//使用System.arraycopy拷贝二维数组,接受的数组也必须是二维数组。
int[][] Darr_test = {{197,56}, {7}, {63,98,100}};
int[][] copy_arr = new int[4][3];
System.arraycopy(Darr_test, 0, copy_arr, 0, Darr_test.length);
System.out.println(Arrays.toString(copy_arr));
System.out.println();
System.out.println();
for(int i=0; i<copy_arr.length; i++){
System.out.println("*********");
String s = Arrays.toString(copy_arr[i]);
System.out.println(s);
}
System.out.println();
System.out.println();
//使用Arrays.copyOf拷贝二维数组,接受的数组也必须是二维数组。
int[][] arr1 = Arrays.copyOf(Darr_test, Darr_test.length+1);
for(int[] arr:arr1){
System.out.println(Arrays.toString(arr));
}
System.out.println();
//对二维数组使用sort方法,不能直接对二维数组进行排序,
//因为二维数组里面的元素不是可排序对象
for(int i=0; i<Darr_test.length; i++){
Arrays.sort(Darr_test[i]);
}
// 反向排序
// for(int i=Darr_test.length-1; i>=0; i--){
// Arrays.sort(Darr_test[i]);
// }
// Arrays.sort(Darr_test);
System.out.println(Arrays.toString(Darr_test));
for(int i=0; i<Darr_test.length; i++){
System.out.println(Arrays.toString(Darr_test[i]));
}
System.out.println("*********");
// 反向排序
// for(int i=Darr_test.length-1; i>=0; i--){
// System.out.println(Arrays.toString(Darr_test[i]));
// }
//equals方法不能直接比较两个二维数组是否相等,因为它比较的是内存地址。
int[][] arr_equal1 = {{79,3},{80,1,4,9},{7},{0,0,0}};
int[][] arr_equal2 = {{79,3},{80,1,4,9},{7},{0,0,0}};
boolean isEqual = Arrays.equals(arr_equal1, arr_equal2);
// System.out.println(arr_equal1);
// System.out.println(arr_equal2);
// System.out.println("arr_equal1:"+Arrays.toString(arr_equal1));
// System.out.println("arr_equal2:"+Arrays.toString(arr_equal2));
System.out.println(isEqual);
//fill方法不能直接用于二维数组
// int[] arr_fill = new int[3];
// int[] small_arr = new int[1];
// Arrays.fill(arr_fill, small_arr);
int[][] test_fill = new int[3][2];
for(int i=0; i<test_fill.length; i++){
Arrays.fill(test_fill[i], 100);
System.out.println(Arrays.toString(test_fill[i]));
}
// Arrays.fill(test_fill, 100);
// for(int i=0; i<test_fill.length; i++){
// System.out.println(Arrays.toString(test_fill[i]));
// }
//Arrays.binarySearch(a, key)方法不能直接用于二维数组,因为二维数组无法直接排序。
}
}