Jakkie Chan Jakkie Chan - 6 months ago 19
Java Question

Inserting to an ArrayList of LinkedList

Constructor:

array = new ArrayList<LinkedList<String>>(size);


Trying to insert with this line:

array[index].add(value);


It return an error saying "The type of the expression must be an array type but it resolved to ArrayList<\LinkedList<\String>>"

I'm trying to insert a string into a linkedlist node in an array index using the add linkedlist method. Why isn't that line working?

*This is a hashmap implementation btw, an array of linked lists

Answer

Arrays use the [] operator. Other types (such as Lists) can't, and must use methods (such as get) to access their elements:

array.get(index).add(value);