Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #284 Create a zeroed list of integers

Create a new list a (or array, or slice) of size n, where all elements are integers initialized with the value 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);
import java.util.*;
List<Integer> a = new ArrayList<Integer>(n);
for (int i =0;i<n;i++){
    a.add(0);
}
import java.util.stream.IntStream;
int[] a = IntStream.generate(() -> 0)
                .limit(n)
                .toArray();
int[] a = new int[n];
a := nil;
setlength(a, n);
my @a = (0) x $n;
a = [0] * n
a = [0] * n
a = Array.new(n, 0)
let a = vec![0; n];

New implementation...
< >
programming-idioms.org