Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- C
- Clojure
- C++
- C#
- Dart
- Fortran
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Java
- Java
- Java
- Java
- Lua
- Pascal
- Perl
- Python
- Python
- Ruby
- Ruby
- Rust
int *a = calloc(n, sizeof(*a));
calloc automatically sets every byte allocated to 0.
std::vector<int> a(n, 0);
var a = new int[n];
The default value for integers is zero, so we make an array of integers at the right size to get an empty set.
var a = List.filled(n, 0);
integer, dimension(:), allocatable :: a
allocate(a(n),source = 0)
a = replicate n 0
const a = new Array(n).fill(0);
int a[] = new int[n];
The default `int` value in Java, is 0.
int[] a = new int[n];
Java automatically initializes integer arrays with zeros.
int a[] = iterate(0, i -> 0).limit(n).toArray();
local function new_zeroed_list(n)
local ret={}
for i=1,n do
ret[i]=0
end
return ret
end
local zeroed_list=new_zeroed_list(n)
a := nil;
setlength(a, n);
First clear the array, then set its length. This ensures data is zero-ed.
a = [0] * n
a = Array.new(n, 0)
a = [0] * n
let a = vec![0; n];