-13

This is the python 2.7 code and its output

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

output:

[0, 0, 0, 0, 0]

what is the closest code to do this in Java?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Adh Far
  • 41
  • 6

1 Answers1

1

Here is the closest code:

public class Stackoverflow_03262019 {
    public static void main(String[] args) {
        int[] arr=new int[5];
        Arrays.fill(arr,0);
        Arrays.stream(arr).forEach(val-> System.out.println(val));
    }
}
```
You can add any value instead of 0; 

Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42
  • 5
    Just a note the arrays do not need to be filled with 0's. The array in Java will be set to the values default value, numbers = 0, bools = false and objects = null. [SO - Default Array Initialization](https://stackoverflow.com/questions/3426843/what-is-the-default-initialization-of-an-array-in-java) – Mr00Anderson Mar 26 '19 at 18:13
  • 1
    Actually only the int[] arr=new int[5]; would solve it since 0 is the default. – GaboSampaio Mar 26 '19 at 18:16