0

Consider the following case. I have 2 classes as shown below:

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*/

  }

Now there is another class in which I have created ArrayList[][] of CustomObject, the way shown below

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;
      }

 }

I could not find a way to do this type of assignment. Please comment if you need more info!

4
  • 1
    new ArrayList[100][100] ??? Commented Apr 11, 2017 at 10:39
  • @ANS It's a multidimensional array of ArrayList. It compiles. Commented Apr 11, 2017 at 10:50
  • @VinceEmigh It does not compile fine. It compiles with warnings. Commented Apr 11, 2017 at 16:14
  • @TomHawtin-tackline I didn't say fine, I just said it compiles, as in "the grammar allows it". Commented Apr 11, 2017 at 18:53

1 Answer 1

1

Object referenced by arrCO[indx][indy] is an ArrayList

arrCO is a 2-D array of List of CustomObject

do this to access what you're trying to access:

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

Now you can access arrInt & objectSOC as

customObj.arrInt & customObj.objectSOC
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I was interpreting the declaration itself in a faulty manner. Just to add to it a bit, if anyone wants to further visualise it a bit in their head, viewing variable in Debugger mode in any IDE, NetBeans/Eclipse might help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.