1

I am here trying to get an element from my two dimensional ArrayList but getting IndexOutOfBoundException error. What am I doing wrong here? Do I need to allocate the space like in a simple Array first? If so, How can I do it in two dimensional array? Below is the code,

import java.util.ArrayList;

public class Test {
    private ArrayList<ArrayList<String>> array;

    public Test(){
        array = new ArrayList<ArrayList<String>>();
        array.get(0).set(0, "00");
        array.get(0).set(1, "01");
        array.get(0).set(2, "02");
        array.get(1).set(0, "10");
        array.get(1).set(1, "11");
        array.get(1).set(2, "12");
        array.get(2).set(0, "20");
        array.get(2).set(1, "21");
        array.get(2).set(2, "22");
    }

    public String getE(int a, int b){
        return array.get(a).get(b);
    }

    public static void main(String[] args) {
        Test object = new Test();
        System.out.println(object.getE(0, 0)); // This gives me the error.
    }
}

1 Answer 1

3

You need to initialize the ArrayLists before you insert into them. Like this:

public Test(){
    array = new ArrayList<ArrayList<String>>();
    array.add(new ArrayList<String>());
    array.add(new ArrayList<String>());
    array.add(new ArrayList<String>());
    array.get(0).add("00");
    array.get(0).add("01");
    array.get(0).add("02");
    array.get(1).add("10");
    array.get(1).add("11");
    array.get(1).add("12");
    array.get(2).add("20");
    array.get(2).add("21");
    array.get(2).add("22");
}

Right now, array is empty, so calling array.get(0) will result in the IndexOutOfBoundException. Once you add an initialized ArrayList at index 0, you will no longer get that error.

The constructor ArrayList<ArrayList<String>>() makes an empty ArrayList<ArrayList<String>>. It does not initialize the contents of array; you need to do that yourself.

Additionally, using set(int i , E e) gives you the same problem if there is not already an element at index i. You should use add(E e) instead. You can read more about set() at http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set(int, E).

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

6 Comments

Didn't I do it at the constructor? Aren't I initializing the ArrayList with this line of code array = new ArrayList<ArrayList<String>>();?
Oh I get it. Do I have to initialize a new ArrayList for every row to get two dimensional array list?
@Zip You are making an empty ArrayList<ArrayList<String>> with the constructor. You need to add initialized ArrayList<String>s to array.
I am still getting the error though after adding to three line of codes.
@Zip see my most recent update. You are getting errors because of set().
|

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.