Home > Net >  Printing from Arraylist<Integer[]>
Printing from Arraylist<Integer[]>

Time:12-29

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]);
    }

CodePudding user response:

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

CodePudding user response:

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]);
    }
}
  • Related