How to Initialize an Arraylist with All Zeroes in Java

How can I initialize an ArrayList with all zeroes in Java?

The integer passed to the constructor represents its initial capacity, i.e., the number of elements it can hold before it needs to resize its internal array (and has nothing to do with the initial number of elements in the list).

To initialize an list with 60 zeros you do:

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

If you want to create a list with 60 different objects, you could use the Stream API with a Supplier as follows:

List<Person> persons = Stream.generate(Person::new)
.limit(60)
.collect(Collectors.toList());

Initialize an arrayList with zeros

You can use Collections.fill(List<? super T> list,T obj) method to fill your list with zeros. In your case you are setting new ArrayList<>(40) here 40 is not length of the list but the initial capacity. You can use array to build your list with all zeros in it. Checkout following piece of code.

ArrayList<Integer> myList= new ArrayList<>(Arrays.asList(new Integer[40]));
Collections.fill(myList, 0);//fills all 40 entries with 0"
System.out.println(myList);

OUTPUT

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Initiallizing Values of ArrayListInteger with zeroes - Java

Change array.add(i, 0) to array.add(0);.

You cannot add to those specified indices unless the size of the ArrayList is larger or equal since those indices don't exist.

Also you'll have to change your for loop as well since its size is not 100; it is 0. This is because when you declare array = new ArrayList<Integer>(100), you're only specifying how much memory should be reserved for it - not its size. Its size is still 0 and will grow only when you add elements.

Fill a 2D ArrayList with zeros

What about the following?

Stream.generate(() -> new ArrayList<Double>(Collections.nCopies(10, 0.0)))
.limit(10)
.collect(Collectors.toList());

Initial size for the ArrayList

You're confusing the size of the array list with its capacity:

  • the size is the number of elements in the list;
  • the capacity is how many elements the list can potentially accommodate without reallocating its internal structures.

When you call new ArrayList<Integer>(10), you are setting the list's initial capacity, not its size. In other words, when constructed in this manner, the array list starts its life empty.

One way to add ten elements to the array list is by using a loop:

for (int i = 0; i < 10; i++) {
arr.add(0);
}

Having done this, you can now modify elements at indices 0..9.

How to initialize ArrayList with same object x times?

Use Collections.nCopies, and copy it into an ArrayList:

private <T> List<T> initStringArrayList(int size, T s) {
return new ArrayList<>(Collections.nCopies(size, s));
}

This assumes that you really do want a mutable List at the end. If you can make do with an immutable list with the item size times, Collections.nCopies(size, s) by itself would work: it is memory-efficient, fast to allocate etc.

Java: Initialize an array of 0.0 or 1 or A with size n*1 or size n*m

To make it into a one-liner, you can make a simple method, that does it.
You can pass the size and value and return the new ArrayList:

static ArrayList<String> populateArrayList(int size, String defaultValue) {
ArrayList<String> list = new ArrayList<String>();
for (int i=0; i<size; i++)
list.add(defaultValue);
return list;
}

If you wish to use static list - array, then similar code can be applied:

static String[] populateArray(int size, String defaultValue) {
String[] array = new String[size];
for (int i=0; i<size; i++)
array[i] = defaultValue;
return array;
}

And then to use, just call the method and assign its returned value to a new list:

ArrayList<String> data1 = populateArrayList(100, "Doggo");
String[] data2 = populateArray(100, "Moo-moo");

Default values

As stated in comments, variables of different data types have their default values that are used in arrays, object params etc.

  • for numerics it is 0,
  • for boolean - true,
  • for String and other type it is null

More about default values here

Clean way to initialize an arraylist

You can use Collections.nCopies:

ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(100, 10));

This will initialize list with 100 10's.



Related Topics



Leave a reply



Submit