![TestAO TestAO]()
这是示例代码:class TestAO{ int[] x; public TestAO () { this.x = new int[5] ; for (int i = 0; i<x.length; i++) x[i] = i; } public static void main (String[]arg) { TestAO a = new TestAO (); System.out.println (a) ; TestAO c = new TestAO () ; c.x[3] = 35 ; TestAO[] Z = new TestAO[3] ; Z[0] = a ; Z[1] = (TestAO b = new TestAO()) ; Z[2] = c ; }}当我尝试对此进行编译时,我在行Z[1]上收到一条错误消息,内容如下:TestAO.java:22: ')' expected Z[1] = (TestAO b = new TestAO()) ; ^我在这里要做的是创建对象TestAO的实例,该实例要在该索引的值赋值中位于该索引中,而不是像在。这合法吗,我只是犯了一些我看不到的语法错误(因此导致了错误消息),或者我试图做的只是无法完成?编辑:关于马克的回答,这里是我的后续问题:是否有比以下方法更短的方法将值分配给对象数组中的对象的实例变量:(无需编写任何特殊的构造函数) Z[1] = new TestAO() ; Z[1].x[4] = 80085 ; (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 它比您想象的要容易:Z[1] = new TestAO();关于java - 在对象数组中,可以在索引处创建对象的新实例吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2502660/ (adsbygoogle = window.adsbygoogle || []).push({}); 10-09 01:14