简体   繁体   中英

Printing from Arraylist<Integer[]>

I need to save info in an Arraylist<Integer[]> type array list named "path". How do I print the values in each position of the arraylist?

ArrayList<Integer[]> path = new ArrayList<Integer[]>(6);
    Integer[] usedPath = new Integer[3];
    for(int i = 0; i < 6; i++){
        for(int j = 0; j < 3; j++){
            usedPath[j] = i * 10 + 1;
        }
        path.add(usedPath);
    }
    for(int i = 0; i < 6; i++){
        System.out.println(path.get(i)[0] + "  " + path.get(i)[1] + "  " + path.get(i)[2]);
    }

you can use nested loop same as when you fill your array

for(int i = 0; i < 6; i++){
    for(int j = 0; j < 3; j++){
        System.out.println(path.get(i)[j]);
    }
}

or use

for(int i = 0; i < 6; i++){
        System.out.println(Arrays.asList(path.get(i)));
    }

or use stream

 path.stream().map(Arrays::asList).forEach(System.out::println);

it;s not recommend to use for each that has outside effect

you need first iterate over outer Array then you should iterate over inner array, something like below

for(Integer[] integerArray : path){

   
  for(int i=0;i<=integerArray.length;i++){
     System.out.println(integerArray[i]);
    }
}

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