简体   繁体   中英

Java: ArrayLists of ArrayList

I have Array of arrays how can I print all values that are inside of this Array?

 ArrayList<ArrayList<String>> alltransactions = new ArrayList<ArrayList<String>>();
 alltransactions.add(Transaction);

Maybe this could help you. A simple method to print array within an array using Arrays.deepToString() .

ArrayList <ArrayList> outer = new ArrayList<ArrayList>();
ArrayList <Integer> inner1 = new ArrayList<Integer>();
ArrayList <Integer> inner2 = new ArrayList<Integer>();

for (int i = 0; i < 10; i++) {
    inner1.add(i);
    inner2.add(i + 10);
}

outer.add(inner1); 
outer.add(inner2);
System.out.println(Arrays.deepToString(outer.toArray()));

Output:

[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] ]

List<String> list1 = new ArrayList<>();
List< List<String> > list_all = new ArrayList<>();
list_all.add(list1);
for(List<String> list : list_all){
  for(String s : list){
   System.out.println(s);
  }
}

Why am I using List instead of ArrayList? Code to interface, not to implementation.

You can iterate at 2 levels and print the contents of list as below:

List<String> Transaction = new ArrayList<String>();
List<ArrayList<String>> alltransactions = new ArrayList<ArrayList<String>>();
alltransactions.add(Transaction);
for (ArrayList<String> ar : alltransactions)
{
    for(String str : ar)
    {
        System.out.println(str);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM