Logo

Programming-Idioms

Create a new list a (or array, or slice) of size n, where all elements are integers initialized with the value 0.
New implementation

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.

Other implementations
#include <stdlib.h>
int *a = calloc(n, sizeof(*a));
(def a (repeatedly n #(identity 0)))
std::vector<int> a(n, 0);
var a = new int[n];
var a = List.filled(n, 0);
integer, dimension(:), allocatable :: a

allocate(a(n),source = 0)
a := make([]int, n)
a = replicate n 0
const a = new Array(n).fill(0);
int a[] = new int[n];
import static java.util.Arrays.fill;
Integer a[] = new Integer[n];
fill(a, 0);
int[] a = new int[n];
import static java.util.stream.IntStream.range;
int a[] = range(0, n).map(i -> 0).toArray();
import java.util.stream.IntStream;
int[] a = IntStream.generate(() -> 0)
                .limit(n)
                .toArray();
import java.util.*;
List<Integer> a = new ArrayList<Integer>(n);
for (int i =0;i<n;i++){
    a.add(0);
}
import static java.util.stream.IntStream.iterate;
int a[] = iterate(0, i -> 0).limit(n).toArray();
import static java.util.stream.IntStream.range;
import java.util.ArrayList;
import java.util.List;
List<Integer> a = new ArrayList<>(n);
range(0, n).forEach(i -> a.add(0));
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);
my @a = (0) x $n;
a = [0] * n
from itertools import repeat
a = list(repeat(0, n))
a = Array.new(n, 0)
a = [0] * n
let a = vec![0; n];