Raul Quinzani Raul Quinzani - 5 months ago 9
Java Question

add Objects in a bidemensional arrayList of size unknow JAVA

i'm trying to add a element in an arrayList that is inside a arraylist,
when i had elements before i used to get a element by putting

ListName.get(index1).get(index2);


and it returns to me whatever is inside this coordenates

i'm trying to add objects while my biArrayList is empty, so i have this:

private List<List<Cadeira>> lista = new ArrayList<List<Cadeira>>();
lista.get(0).add(myObject);


however i'm getting in a exception because my position 0 does not exist yet

any ideia on how to solve this??
ty in advance

Answer

You need to initialize the list first with a constructor. Do

lista.add(new ArrayList<Cadeira>());
lista.get(0).add(myObject);
Comments