0

I want to use multidimentional arraylist for my work. Please help me to solve this problem. My code is:

public class newType {
  String name;
  int id;

  public newType() {
    name="";
    id=0;
  }

  public newType(String name,int id) {
    this.name=name;
    this.id=id;
  }
}

public class MainClass {
  public static void main (String[] args) throws Exception {    

   HashMap<String, ArrayList<ArrayList<newType>>> temp=new HashMap<String,   ArrayList<ArrayList<newType>>>();

    ArrayList<ArrayList<newType>> arrObj=new ArrayList<ArrayList<newType>>();

    arrObj.clear();
    arrObj.add(new ArrayList<ValueType>());
    arrObj.add(new ArrayList<ValueType>());
    arrObj.add(new ArrayList<ValueType>());
    arrObj.get(0).add(new newType("Jhon",1));
    arrObj.get(1).add(new newType("get",1));
    arrObj.get(1).add(new newType("book",1));
    arrObj.get(2).add(new newType("coma",1));
    arrObj.get(2).add(new newType("brother",1));
    temp.put("Jhon", arrObj);

    arrObj.clear();
    arrObj.add(new ArrayList<newType>());
    arrObj.add(new ArrayList<newType>());
    arrObj.add(new ArrayList<newType>());
    arrObj.get(0).add(new newType("Mikel",1));
    arrObj.get(0).add(new newType("son",,2));
    arrObj.get(1).add(new newType("puts",2));
    arrObj.get(1).add(new newType("his",1));
    arrObj.get(2).add(new newType("when",1));
    arrObj.get(2).add(new newType("hich",1));
    temp.put("Mikel", arrObj);

    arrObj.clear();
    arrObj=temp.get("Mikel");
    System.out.println(arrObj.get(0).get(0).name);
  }
 }

When I run this program, compiler give me following error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at MainClass.main(MainClass.java:47)

The error is related to arrObj.get(0).get(0).name

How can I solve it?

3
  • One array list and many dimensions seems impossible! Commented Jul 14, 2014 at 19:20
  • @RomanC I am not sure what you mean. Isn't it same rule as with arrays? Two-dimensional array is just one dimensional array of other one dimensional arrays. Same rule can be applied to Lists (OP is using one ArrayList to store other ArrayLists). Commented Jul 14, 2014 at 19:25
  • @Pshemo The list is not an array and name things their own names. Commented Jul 14, 2014 at 19:28

1 Answer 1

4

When you put to your HashMap, it doesn't add a copy of your 2D ArrayList, it adds the reference to it. But you are clearing the 2D ArrayList each time you add it, so you wind up with 2 references to the same empty 2D ArrayList by the time you query its contents.

Create a new 2D ArrayList each time, so you can add separate objects each time. Instead of

arrObj.clear();

Try

arrObj = new ArrayList<ArrayList<newType>>();

(There's no need to clear() the last time.)

Sign up to request clarification or add additional context in comments.

Comments

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.