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.

4
  • to create an array of integers for example initializing, java does with zeros Commented Sep 18, 2016 at 22:18
  • @Dev.that's true for int[] but not for ArrayList<Integer>. Commented Sep 18, 2016 at 22:21
  • Possible duplicate of Any shortcut to initialize all array elements to zero? Commented Sep 18, 2016 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. Commented Sep 19, 2016 at 0:08

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

What exactly does the "cant" mean in the two variables declared at the top?
Is the number of rows and columns of your multidimensional array
So pretty much cantRows is ROWS and cantColumns is COLUMNS in my code?
If it is, change my answer to that is more understandable
Integer.valueOf(0) may save heap than new Integer(0)
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);
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.