Reputation: 39
So I got this code:
List chunks = new ArrayList<ArrayList<Block>>(1);
And I need to get a child of an inner ArrayList.
Upvotes: 1
Views: 1148
Reputation: 121998
First fix the declaration part
List<List<Block>> chunks = new ArrayList<ArrayList<Block>>(1);
or even better if you are using Java 7
List<List<Block>> chunks = new ArrayList<>(1);
And then
chunks.get(0).get(0);
Will give you the inner element Block
, assuming you have already added the elements in it.
If no elements added at that place, you'll run into exception.
Upvotes: 7
Reputation: 393841
List<List<Block>> chunks = new ArrayList<ArrayList<Block>>(1);
creates an empty ArrayList
, so you can't get
anything from it.
Note that I changed the type of chunks
. If you use a raw type (i.e. List
), chunks.get()
will return an instance of Object
type, and you'll have to cast it (unsafely) to List<Block>
in order to obtain elements from the inner List
.
First you have to add something to it :
chunks.add (new ArrayList<Block>());
Then you can get the inner ArrayList
and add
an element to it :
chunks.get(0).add (new Block());
Then you can obtain the inner element via :
Block b = chunks.get(0).get(0);
Upvotes: 4