我正在尝试创建一个Java对象数组并将数组放在其第二个索引中(为了表示与数组的自相似分形),但是当我尝试访问theArray[1][1][0]
时,我收到这个错误:
Main.java:11: error: array required, but Object found
。
这是我到目前为止所尝试过的,而且我不确定它为什么不起作用:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Object[] theArray = new Object[2];
theArray[0] = "This array should contain itself at its second index.";
theArray[1] = theArray; //Now I'm attempting to put the array into itself.
System.out.println(theArray[1][1][0]) //Main.java:11: error: array required, but Object found
}
}
实际上是否可以在自己内部放置一个Java数组,正如我试图在这里做的那样?
答案 0 :(得分:19)
theArray[1]
属于编译时类型Object
(因为它来自对象的数组)。
您需要将其强制转换为Object[]
才能将其用作数组。
您遇到的基本问题是,虽然包含自身的数组是完全有效的对象,但它不是有效的类型。
您可以任意深度嵌套数组类型 - Object[][][][][][][][][][][][][]
是有效类型
但是,该类型的“底层”不能是数组。
您正在尝试创建一个本身数组的类型 使用泛型,这是可能的:
class Evil extends ArrayList<Evil> { }
答案 1 :(得分:2)
由于您已将theArray
声明为对象数组,因此您遇到了转换错误。因此,您无法承诺theArray[1]
是Array
的Java - 它可以是任何类型的Object
。您需要打破访问权限以执行您想要的操作:
Object[] innerArray = (Object[]) theArray[1];
System.out.println(innerArray[0] == theArray[0]); // Always true since innerArray IS theArray
while (true) {
// Careful! This loops forever!
// set innerArray = innerArray[1] = theArray = theArray[1] = innerArray...
// all of these are the exact same object (but you have to tell Java their type every time)
innerArray = (Object[]) innerArray[1];
System.out.println(innerArray[0]);
}
答案 2 :(得分:1)
您的代码等同于
Object arr = theArray[1]; // arr is an Object here, not an array
但你可以做到
Object[] arr = (Object[] ) theArray[1]; // Now it is an array
答案 3 :(得分:0)
使用ArrayLists可以很容易地做到这一点:
ArrayList list = new ArrayList();
list.add(list);
所以现在
System.out.println(list.get(0));
和
System.out.println(((ArrayList)list.get(0)).get(0)); //Casting because .get() returns an Object
两者都会输出相同的内容。
如果需要,您可以将其扩展到任意多个级别。
System.out.println(((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)list.get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0));