1

What is the equivalent in Java to this in Python?

array_name = [0 for i in range(5)]

Specifically, I want to create a 2 dimensional Java ArrayList and initialize each value as 0. This is what I was thinking, in semi-psuedocode, following Python, but it obviously doesn't work:

ArrayList<ArrayList<Integer>> grid = new ArrayList<ArrayList<Integer>>() {{
    (add(Arrays.asList(0 for i : ROWS) for j : COLUMNS);
}};

Where ROWS and COLUMNS are constants defined elsewhere. In the end, I want a 2D ArrayList, with sizes given by the ROWS and COLUMNS constants, and each value initialized as 0. Is there a way to do this?

And ArrayLists don't allow primitive types as the generic type (like the primitive int). Then having the reference type doesn't have a default of 0 since it is an object. So any other questions concerning the initialization of an array to 0 that have to do with the primitive int don't answer mine.

  • to create an array of integers for example initializing, java does with zeros – Dev. Joel Sep 18 '16 at 22:18
  • @Dev.that's true for ``int[]`` but not for ``ArrayList``. – f1sh Sep 18 '16 at 22:21
  • Possible duplicate of [Any shortcut to initialize all array elements to zero?](http://stackoverflow.com/questions/2154251/any-shortcut-to-initialize-all-array-elements-to-zero) – Natecat Sep 18 '16 at 22:21
  • Like @f1sh says, the possible duplicate is right for the primitive int and array, not my situation with the Integer object for an ArrayList. – Shivashriganesh Mahato Sep 19 '16 at 00:08

3 Answers3

1
ArrayList<ArrayList<Integer>> grid = new ArrayList<>();
for (int i = 0; i < ROWS; i ++) {
    ArrayList<Integer> row = new ArrayList<>();
    for (int j = 0; j < COLUMNS; j ++) {
        row.add(Integer.valueOf(0));
    }
    grid.add(row);
}
auntyellow
  • 2,423
  • 2
  • 20
  • 47
1

To initialize a ArrayList with zeros could make use of the method nCopies

    int ROWS =3;
    int COLUMNS=3;
    List<List<Integer>> twoDimArray = new ArrayList<List<Integer>>();//declaration
    for (int i = 0; i < ROWS ; i++) { // cant rows 
        twoDimArray.add(Collections.nCopies(COLUMNS, new Integer(0)));
        //initialized to zero the number of columns you want
    }

    /* Print  */

    for (List<Integer> list : twoDimArray) {
        System.out.println(list);
    }

In the example use the List <> to replace ArrayList as it is much better ... and for references to this review Type List vs type ArrayList in Java

Community
  • 1
  • 1
Dev. Joel
  • 1,127
  • 1
  • 9
  • 14
0

In java 8, the equivalent would be:

List<List<Integer>> grid = Stream.generate(() -> Collections.nCopies(COLUMNS, 0))
                                 .limit(ROWS)
                                 .collect(Collectors.toList());

Note that you cannot modify the list returned by Collections.nCopies, so a better alternative might be:

List<List<Integer>> grid = Stream.generate(() -> new ArrayList<>(Collections.nCopies(COLUMNS, 0)))
                                 .limit(ROWS)
                                 .collect(Collectors.toList());

If you really need the type of the grid to be ArrayList<ArrayList>, you do:

ArrayList<ArrayList<Integer>> grid = Stream.generate(() -> new ArrayList<>(Collections.nCopies(COLUMNS, 0)))
                                           .limit(ROWS)
                                           .collect(Collectors.toCollection(ArrayList::new));
smac89
  • 39,374
  • 15
  • 132
  • 179