从方法签名可以很清楚地看出,方法接受Object[]类型的参数,但此方法仍然可以很好地用于原始多维数组?(如int[][]、Float[][]等) (但是,对于原始一维数组(如int[]、Float[]等),它会显示错误)
例:
int[][]arr=new int[2][3];
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
int[][]arr1=new int[2][3];
arr1[0][0]=1;
arr1[0][1]=2;
arr1[0][2]=3;
arr1[1][0]=4;
arr1[1][1]=5;
arr1[1][2]=6;
System.out.println(Arrays.deepEquals(arr,arr1));
Output: true
例:
int[]arr={1,2,3,4,5};
int[]arr1={1,2,3,4,5};
System.out.println(Arrays.deepEquals(arr,arr1));//Error
/*"The method deepEquals(Object[], Object[]) in the type Arrays is not applicable for the arguments (int[], int[])"*/
方法签名中的Arrays.DeeptoString(Object[])也存在同样的问题.很明显,此方法也接受类型Object[]的参数,但对于原始多维数组(如int[][],Float[][]等),它仍然工作得很好(但对于原始一维数组(如int[],Float[]等),它显示错误).
例:
int[][]arr=new int[2][3];
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
System.out.println(Arrays.deepToString(arr));
Output: [[1, 3, 0], [4, 5, 6]]
例:
int[]arr={1,2,3,4,5};
System.out.println(Arrays.deepToString(arr)); //Error
//"The method deepToString(Object[]) in the type Arrays is not applicable for the arguments (int[])"