Java: Initialize List With Zeroes - Programming.Guide

Featured Stack Overflow Post

In Java, difference between default, public, protected, and private StackOverflow screenshot thumbnail

Top Java Articles

  1. Do interfaces inherit from Object?
  2. Executing code in comments?!
  3. Functional Interfaces
  4. Handling InterruptedException
  5. Why wait must be called in a synchronized block

See all 190 Java articles

Top Algorithm Articles

  1. Dynamic programming vs memoization vs tabulation
  2. Big O notation explained
  3. Sliding Window Algorithm with Example
  4. What makes a good loop invariant?
  5. Generating a random point within a circle (uniformly)
Java: Initialize list with zeroes

Here's how:

List<Integer> list = Collections.nCopies(1337Desired number of zeroes, 0);

Note that nCopies returns an immutable list. To initialize for instance an ArrayList, use this:

List<Integer> list = new ArrayList<Integer>(Collections.nCopies(1337, 0));

Comments

Be the first to comment!

Tag » How To Initialize A List In Java