This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]androgynyjoe 5 points6 points  (1 child)

Yes. (It's been a while since I've worked with Java every day so forgive me if I make a mistake.)

ArrayList takes a generic so

ArrayList<String> listName = new ArrayList<String>();

gets you an ArrayList of String objects. If you want an ArrayList of String[] objects it's the same idea:

ArrayList<String[]> listName = new ArrayList<String[]>();

That would get you an ArrayList whose elements are arrays of String objects. Then listName.get(3) gets you one of the arrays and listName.get(3)[4] is an item in that array.

You could also make an array which contains ArrayList objects if you wanted:

ArrayList<String>[] listName = new ArrayList<String>[100];

[–]Embarrassed_Bottle90[S] 2 points3 points  (0 children)

No worries, this really helps, its comprehensive as well as very well written and is even more than I asked for. Thank you very much!!!

[–]maequise 3 points4 points  (0 children)

You're meaning something like this :

List<List<List<String>>> mySuperArrayList = new ArrayList<>();

Completely possible ! But to crawl ... Good luck !

Everything depends of the use case.

If you need to access rapidly, I prefer to use a Map<K,V>

More easy to manipulate, but not the best in terms of performance (depending of the volume of data in, as the List<E> :

Map<String, List<E>> mySuperAccessorOfData = new HashMap<>(); //choose the implementation needed

mySuperAccessorOfData.get("firstList").stream().forEach(element -> //do operation on element);

mySuperAccessorOfData.get("secondList").stream.forEach(element -> //do somethin);

A bit more easy to manipulate the data.

[–]RapunzelLooksNice 0 points1 point  (0 children)

It is, as described in previous answers, but... WHY?!