[英]Multiple types on a Java multi-dimensional array
From what I know, only one type of object/literal can be stored on a multi-dimensional Java array. 据我所知,多维Java数组上只能存储一种类型的对象/文字。
so for instance, on a 2-dim'l array a[][], I can not store a Type-1 array on a[0] and Type-2 array on a[1] unless there's some polymorphic relationship between Type-1 & Type-2 and I put it into use there. 因此,例如,在2维数组a [] []上,除非Type-之间存在某种多态关系,否则我无法在a [0]上存储Type-1数组,在a [1]上存储Type-2数组。 1和Type-2,我在那里使用它。
I'm looking to verify that there's no way to get around this. 我正在尝试验证没有办法解决此问题。 so, I cannot somehow put an int array on a[0], a char array on a[1]-- Java arrays are single-type. 因此,我无法以某种方式将int数组放在a [0]上,将char数组放在a [1]上-Java数组是单类型的。
I'm aware that I can declare 2 parallel arrays-- one int[] and one char[] and that solves it. 我知道我可以声明2个并行数组-一个int []和一个char []即可解决这个问题。
Thanks in advance. 提前致谢。
//===================== // =====================
Edit: The good old object class solves it-- as the useful answers below pointed out. 编辑:良好的旧对象类可以解决此问题,如下面指出的有用答案。 Thank you for your input. 谢谢您的意见。
You can use Object[]
. 您可以使用Object[]
。
Object[] arrays = new Object[2];
arrays[0] = new int[10];
arrays[1] = new char[10];
You can use an array
of Objects: 您可以使用对象array
:
Object[] x = new Object[2];
x[0] = new Integer[3];
x[1] = new String[3];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.