在Java中可以做到这一点吗?
这是Python2.7代码及其输出在Java中可以做到这一点吗?,java,python,Java,Python,这是Python2.7代码及其输出 i = [0 for i in range(5)] 输出: [0, 0, 0, 0, 0] 在Java中,最接近的代码是什么?以下是最接近的代码: public class Stackoverflow_03262019 { public static void main(String[] args) { int[] arr=new int[5]; Arrays.fill(arr,0); Arrays.
i = [0 for i in range(5)]
输出:
[0, 0, 0, 0, 0]
在Java中,最接近的代码是什么?以下是最接近的代码:
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;
int[]i=新的int[5]
;Java中没有列表理解。在这种特殊情况下,Collections.nCopies(5,0)
。默认值的巧妙使用。请注意,数组不需要用0填充。Java中的数组将设置为默认值,numbers=0、bools=false和objects=null。实际上只有int[]arr=newint[5];将解决此问题,因为默认值为0。