I want a generic method to print all the elements of a multidimensional array. In the below code i am able to print all the elements of any multidimensional array which belongs to the parent Object class but not of any primitive types. Is it possible to print all elements of a multidimensional array of primitive type. One more doubt if int value can be hold in a Object type then why not int[] can be cast to Object[] but, String[] can be cast to Object[].
我想要一个通用的方法来打印多维数组的所有元素。在下面的代码中,我能够打印属于父Object类但不属于任何基本类型的任何多维数组的所有元素。是否可以打印基本类型的多维数组的所有元素。还有一个疑问,如果int值可以保存在Object类型中,那么为什么int []可以转换为Object []但是,String []可以转换为Object []。
public class MultiDimension {
public static void main(String[] args) {
//final String ar[][][] = {{{"1","2"},{"3","4","5"}},{{"6","7","8"}},{{"9","10"},{"11"},{"12","13","14","15"}}};//new String[1][3][2][2];
Integer intAr[][][][][][] = {{{{{{1},{2},{3}},{{4},{5},{6}}},{{{7}},{{8}}}}}};
recPrintArray(intAr);
}
public static void recPrintArray(Object ar) {
recPrintArray((Object[])ar,getDimensions(ar));
}
public static void recPrintArray(Object[] ar,int noODDimension) {
for (Object obj:(Object[]) ar) {
if (noODDimension > 0)
recPrintArray((Object[])obj, noODDimension - 1);
else {
System.out.print("> " + obj + " ");
}
}
}
/*return the number of dimension of an array
* takes any type as argument
* using the Object class getClass() and Class class getName() methods
*/
public static int getDimensions(Object intAr) {
return intAr.getClass().getName().lastIndexOf("[");
}
}
2 个解决方案
#1
2
To answer your question, we need to introduce the concept of autoboxing first. Primitive types have their class
matches. int
has Integer
, double
has Double
and so on. When a primitive type needs to be handled as an Object
, the compiler will automatically convert the primitive into an instance of its wrapper class
. Since you have an array of Objects
, your primitive values are needed as Objects
, so autoboxing will happen. If you want this to happen in a generic way, then you need to just check whether you have an array and if not, print the Object
by calling its toString
method.
要回答您的问题,我们首先需要介绍自动装箱的概念。原始类型具有类匹配。 int有Integer,double有Double等等。当原始类型需要作为Object处理时,编译器会自动将原语转换为其包装类的实例。由于您有一个对象数组,因此需要将原始值作为对象,因此将进行自动装箱。如果您希望以通用方式进行此操作,则需要检查是否有数组,如果没有,则通过调用其toString方法来打印Object。
As for your second question, you cannot convert a primitive array to an array of Object
s, because your array was allocated for primitive types, not for Object
s, but you can upcast a String
array to an Object
array, because all String
s are Object
s.
至于你的第二个问题,你不能将原始数组转换为Object数组,因为你的数组是为基本类型而不是对象分配的,但你可以将String数组转换为Object数组,因为所有的字符串都是对象。
#2
0
since in java multidimensional arrays are array of array and array is an object. so if i recursively iterate on any array references in the last i will get only one dimensional array which i can type cast explicitly in the type supplied by using getClass().getName()
因为在java中,多维数组是数组的数组,而数组是一个对象。因此,如果我递归迭代最后一个数组引用,我将只获得一维数组,我可以使用getClass()提供的类型显式地转换.getName()
package learning;
public class MultiDimension {
public static void main(String[] args) {
final String ar[][][] = {{{"1","2"},{"3","4","5"}},{{"6","7","8"}},{{"9","10"},{"11"},{"12","13","14","15"}}};//new String[1][3][2][2];
//Integer integerAr[][] = {{1},{2}};
//byte byteAr[][] = {{1},{2}};
//int[] intAr = (int[])byteAr;
recPrintArray(ar);
}
public static void recPrintArray(Object ar) {
recPrintArray(ar,getDimensions(ar));
}
public static void recPrintArray(Object ar,int noOfDimension) {
for (Object obj:(Object[]) ar) {
if (noOfDimension > 1)
recPrintArray(obj, noOfDimension - 1);
else {
String dataType = obj.getClass().getName();
switch (dataType) {
case "[B":
printAll((byte[]) obj);
break;
case "[S":
printAll((short[]) obj);
break;
case "[I":
printAll((int[]) obj);
break;
case "[J":
printAll((long[]) obj);
break;
case "[F":
printAll((float[]) obj);
break;
case "[D":
printAll((double[]) obj);
break;
case "[Z":
printAll((boolean[]) obj);
break;
case "[C":
printAll((char[]) obj);
default:
printAll((Object[]) obj);
}
//System.out.print("> " + obj + " ");
}
}
}
public static void printAll(byte[] ar) {
for (byte val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(short[] ar) {
for (short val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(int[] ar) {
for (int val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(long[] ar) {
for (long val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(float[] ar) {
for (float val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(double[] ar) {
for (double val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(char[] ar) {
for (char val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(boolean[] ar) {
for (boolean val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(Object[] ar) {
for (Object val: ar)
System.out.print(">" + val + " ");
}
/*return the number of dimension of an array
* takes any reference type as argument
* using the Object class getClass() and Class getName() methods
*/
public static int getDimensions(Object intAr) {
return intAr.getClass().getName().lastIndexOf("[");
}
}
#1
2
To answer your question, we need to introduce the concept of autoboxing first. Primitive types have their class
matches. int
has Integer
, double
has Double
and so on. When a primitive type needs to be handled as an Object
, the compiler will automatically convert the primitive into an instance of its wrapper class
. Since you have an array of Objects
, your primitive values are needed as Objects
, so autoboxing will happen. If you want this to happen in a generic way, then you need to just check whether you have an array and if not, print the Object
by calling its toString
method.
要回答您的问题,我们首先需要介绍自动装箱的概念。原始类型具有类匹配。 int有Integer,double有Double等等。当原始类型需要作为Object处理时,编译器会自动将原语转换为其包装类的实例。由于您有一个对象数组,因此需要将原始值作为对象,因此将进行自动装箱。如果您希望以通用方式进行此操作,则需要检查是否有数组,如果没有,则通过调用其toString方法来打印Object。
As for your second question, you cannot convert a primitive array to an array of Object
s, because your array was allocated for primitive types, not for Object
s, but you can upcast a String
array to an Object
array, because all String
s are Object
s.
至于你的第二个问题,你不能将原始数组转换为Object数组,因为你的数组是为基本类型而不是对象分配的,但你可以将String数组转换为Object数组,因为所有的字符串都是对象。
#2
0
since in java multidimensional arrays are array of array and array is an object. so if i recursively iterate on any array references in the last i will get only one dimensional array which i can type cast explicitly in the type supplied by using getClass().getName()
因为在java中,多维数组是数组的数组,而数组是一个对象。因此,如果我递归迭代最后一个数组引用,我将只获得一维数组,我可以使用getClass()提供的类型显式地转换.getName()
package learning;
public class MultiDimension {
public static void main(String[] args) {
final String ar[][][] = {{{"1","2"},{"3","4","5"}},{{"6","7","8"}},{{"9","10"},{"11"},{"12","13","14","15"}}};//new String[1][3][2][2];
//Integer integerAr[][] = {{1},{2}};
//byte byteAr[][] = {{1},{2}};
//int[] intAr = (int[])byteAr;
recPrintArray(ar);
}
public static void recPrintArray(Object ar) {
recPrintArray(ar,getDimensions(ar));
}
public static void recPrintArray(Object ar,int noOfDimension) {
for (Object obj:(Object[]) ar) {
if (noOfDimension > 1)
recPrintArray(obj, noOfDimension - 1);
else {
String dataType = obj.getClass().getName();
switch (dataType) {
case "[B":
printAll((byte[]) obj);
break;
case "[S":
printAll((short[]) obj);
break;
case "[I":
printAll((int[]) obj);
break;
case "[J":
printAll((long[]) obj);
break;
case "[F":
printAll((float[]) obj);
break;
case "[D":
printAll((double[]) obj);
break;
case "[Z":
printAll((boolean[]) obj);
break;
case "[C":
printAll((char[]) obj);
default:
printAll((Object[]) obj);
}
//System.out.print("> " + obj + " ");
}
}
}
public static void printAll(byte[] ar) {
for (byte val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(short[] ar) {
for (short val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(int[] ar) {
for (int val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(long[] ar) {
for (long val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(float[] ar) {
for (float val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(double[] ar) {
for (double val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(char[] ar) {
for (char val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(boolean[] ar) {
for (boolean val: ar)
System.out.print(">" + val + " ");
}
public static void printAll(Object[] ar) {
for (Object val: ar)
System.out.print(">" + val + " ");
}
/*return the number of dimension of an array
* takes any reference type as argument
* using the Object class getClass() and Class getName() methods
*/
public static int getDimensions(Object intAr) {
return intAr.getClass().getName().lastIndexOf("[");
}
}