从Java中的自定义对象的2d Arraylist访问对象

时间:2017-04-11 10:37:57

标签: java generics arraylist multidimensional-array

考虑以下情况。我有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;
      }

 }

我找不到办法做这种类型的作业。如果您需要更多信息,请评论!

1 个答案:

答案 0 :(得分:1)

arrCO [indx] [indy]引用的对象是一个ArrayList

arrCO是List of CustomObject

的二维数组

执行此操作以访问您尝试访问的内容:

np.dot(...)

现在您可以访问arrInt&amp; objectSOC为

List<CustomObject> customObjList = arrCO[indx][indy];
CustomObject customObj = customObjList.get(0)  // assuming there are elements in this list