0

I have got a loop with 2d list where I delete and add elements and want to add those temporary arraylists to the dimensions of another list.

Example,

  // 2d list
  List<List<Integer>> p = new ArrayList<List<Integer>>();
  // 3d list
  List<List<List<List<Integer>>>> list1 = new ArrayList<List<List<List<Integer>>>>();
  // this compiles ok
  list1.get(0).add(p);

but I get the following error:

  java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
2
  • It's simple there is no item in the list1. It's empty. Commented Jun 21, 2014 at 10:27
  • Generally, if you're creating a List<List<List<?>>>, it's time to create some getter / setter classes that each encapsulate a List<?>. Commented Jun 21, 2014 at 14:15

2 Answers 2

1

You need to instantiate every list.. not just the one you are trying to access. Meaning that if you have a 2x2 matrix, you need 2 rows = 2 lists, and another list to hold them both, and so on if the matrix starts getting more complex.

List<List<Integer>> matrix = new ArrayList<List<Integer>>();
List<Integer> row = new ArrayList<Integer>();
matrix.add(row);
Sign up to request clarification or add additional context in comments.

Comments

0

Your "list1" object is actually 4d in your example, but those are not initialized, so when you ask "list1" to get the first (three dimensional) list inside, it does not exist at all (arrays in Java starts at 0 size), so there is nothing to add there.

You could do something like:

List<List<List<List<Integer>>>> list1 = new ArrayList<List<List<List<Integer>>>>();

List<List<List<Integer>>>> 3dlist = new ArrayList<List<List<Integer>>>();

list1.add(3dlist)
list1.get(0).add(p);

Now, using multidimensionnal arrays directly like this is not really practical - you may want to encapsulate them in an object.

3 Comments

Hi, Thanks. This works. However, when I add for example p = [1,2,3],[5,5,5] to list1.get(0).add(p); and then when I change p to equal something else like p = [0,0,0],[7,7,7] and then again I add to the other dimension list1.get(1).add(p); and then print System.out.print(list1.get(0)) it does't print just [1,2,3],[5,5,5] but everything [1,2,3],[5,5,5],[0,0,0],[7,7,7] ? The same is when I print (list1.get(1)). I want to somehow differentiate the temporary 2d lists into the dimensions to access them separately..
This is more and more a design question - StackOverflow is not exactly the place for that. I'm not really sure at what you try to achieve here. If you want general help on your design, you may want to try reddit.com/r/learnprogramming or something like that.
Well. What I want to achieve is quite clear I think. I want an array or any other container where I can store 2d lists. So when I call Container.get(0) it will return all 2d lists I have stored there like [1,2],[3,4] and when I call Container.get(1) it will show me another set of 2d lists like [5,6],[7,8] and so on..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.