Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize an arrayList with zeros

I am creating an ArrayList and its size is 40

ArrayList<Integer> myList= new ArrayList<>(40);

How can I initialize myList with zeros 0 ? I tried this

for(int i=0; i<40; i++){
   myList.set(i, 0);
}

but I get

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
like image 969
yaylitzis Avatar asked Sep 02 '15 10:09

yaylitzis


People also ask

How do you initialize an array in Java with 0?

Using default values in initialization of array For double or float , the default value is 0.0 , and the default value is null for string. Type[] arr = new Type[capacity]; For example, the following code creates a primitive integer array of size 5 . The array will be auto-initialized with a default value of 0 .

How do you initialize all values of an ArrayList in Java?

To initialize an arraylist in single line statement, get all elements in form of array using Arrays. asList method and pass the array argument to ArrayList constructor. ArrayList<String> names = new ArrayList<String>( Arrays.

Can we initialize ArrayList to null?

If your breakpoint is on the initialization line, that line has note yet executed and the value will still be null. The breakpoint is after that line is executed, and that's where it's showing as null . And no, it is not being reinitialized as null later.

Does an ArrayList start at 0 in Java?

Exactly as arrays in all C-like languages. The indexes start from 0.

How do you initialize a list with zeroes in Java?

Java: Initialize list with zeroes. Here's how: List<Integer> list = Collections.nCopies(1337 Desired 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));

How to initialize an array to 0 in Java?

An array is initialized to 0 if the initializer list is empty or 0 is specified in the initializer list. The declaration is as given below: The most simple technique to initialize an array is to loop through all the elements and set them as 0. Video Player is loading. This is a modal window. Beginning of dialog window.

How can I initialize MyList with zeros 0?

Show activity on this post. How can I initialize myList with zeros 0 ? I tried this Show activity on this post. 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.

How do I initialize an ArrayList with ncopies?

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));


1 Answers

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]
like image 139
akash Avatar answered Sep 23 '22 21:09

akash