I have a simple question, can there be something like the following in Java?
ArrayList<String> arr1 = new ArrayList<String>(); ArrayList<String> arr2 = new ArrayList<String>();
If the goal is to iterate over the first array, then the next array.
The best I can understand is to either use separate loops that do redundant coding when the for loops have identical inner code.
Another solution was to create an array of ArrayLists. This facilitates the fulfillment of my intentions, that is:
ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>(); // Initialising arr and populating with data for(ArrayList<String> tempArr : arr) { for(String str : tempArr) { //Do stuff } }
But it does unreadable code. Is there a clean way to do the latter where I don't lose the names of individual arrays?
Thanks in advance, Declan
java arraylist arrays loops foreach
Declan McLeman
source share