考虑以下情况。我有2个类,如下所示:
public class CustomObject{
public int a;
public String xyz;
public ArrayList<Integer> arrInt;
public SomeOtherClass objectSOC;
public CustomObject(){
//Constructor
}
/*Followed by other methods in the class*/
}
现在,在另一个类中,我创建了CustomObject的ArrayList [] [],如下所示
public class CustomObjectUtil{
ArrayList<CustomObject>[][] arrCO = new ArrayList[100][100];
public CustomObjectUtil(){
//Assume there is an object of CustomObject class, let's call it ObjectCO, and a method that adds values to the arrCO using arrCO[i][j].add(ObjectCO);
//Now, here I want to access objects from my 2D ArrayList as
String stringCO = arrCO[indx][indy].xyz;
ArrayList<Integer> arrIntCO = arrCO[indx][indy].arrInt;
SomeOtherClass objectSOC_CO = arrCO[indx][indy].objectSOC;
// But the above method is not allowed;
}
}
我找不到一种进行此类分配的方法。如果您需要更多信息,请发表评论!
最佳答案
arrCO [indx] [indy]引用的对象是一个ArrayList
arrCO是CustomObject列表的二维数组
执行此操作以访问您要访问的内容:
List<CustomObject> customObjList = arrCO[indx][indy];
CustomObject customObj = customObjList.get(0) // assuming there are elements in this list
现在您可以访问arrInt和objectSOC为
customObj.arrInt & customObj.objectSOC