7

The implementation of ArrayList uses Array under the hood. However, Arrays are intialized to default values (0 or null) but ArrayList are just empty. why is this?

       int[] arr = new int[10];
       String[] arr1 = new String[11];
       System.out.println(Arrays.toString(arr));
       System.out.println(Arrays.toString(arr1));
      List<Integer> list = new ArrayList<Integer>(10);
      System.out.println(list);

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

This means every time I use, ArrayList, I need to fill stuff in; I was trying the below part in my code and it was throwing NoSuchElementException and then I realized that it is not defaulted, where as Arrays do

if (list.get(i)==null){
         list.add(i,x);
  else:
        list.add(i,list.get(i)+x)

EDIT:

even List<Integer> list = new ArrayList<Integer>(10);
prints [] although I initialized the size;
4
  • 4
    Regarding your edit: You did not set the size to 10. You set the capacity to 10. The size is 0 until you add an element to it. Commented Jan 29, 2014 at 23:45
  • This means every time I use, ArrayList, I need to fill stuff in ... Yes, you are not using it if you don't "fill stuff in". Commented Jan 29, 2014 at 23:47
  • You see no elements when you print list because its toString() method uses size to determine how many elements print, not capacity of array. When you create ArrayList with initial capacity 10 its size is still 0 because you didn't add any elements to it yet, even if array which stores your objects was initialized as new Object[10] which means it is filled with 10 nulls. Commented Jan 29, 2014 at 23:49
  • @user1988876 P.S. The "capacity" that you initialized the ArrayList with is just a hint, anyway. If you specify the capacity as 10 you can still add 11 elements to it. The results of your program should be the same no matter what capacity you specify; only the performance is affected. Commented Jan 30, 2014 at 0:04

8 Answers 8

12

When constructing an array, the number is the actual size of the array (and you can't change it later). The number in the ArrayList constructor is the initial capacity (space reserved for elements) but the size is zero. The size of an ArrayList can change after it is constructed.

Sign up to request clarification or add additional context in comments.

5 Comments

but it can still default its value to zero or null. how does the dynamic nature of arraylist will affect what it contains
@user1988876 You are calling the toString() method of the arrayList. Since it's size is 0, the String built by toString is empty (check the source code). That's why you "see" nothing.
@GregHewgill Does this mean that even if you initialize the capacity at 10, you can add 20 elements nonetheless? (for an arraylist)
@Xbit Yes. The 10 is only the initial capacity. It can increase afterwards.
@Xbit specifying the initial capacity does not affect its behaviour at all, only its performance
4

When you create an array, you specify the size. This is required because the size of arrays can't be changed after they are created. Something must go in each element of the array, then, and the most obvious thing to put is 0 or null.

On the other hand, ArrayLists are designed to be able to be resized. So you shouldn't have to specify the size when you create them. If the starting size is more then zero, it would have to initialize all those elements, and it's easier not to. So the starting size is zero.

1 Comment

What you initialized is the capacity, not the size. The array list makes room for 10 elements, but that doesn't mean it has 10 elements.
4

You could do it like this:

List<Integer> items = Arrays.asList(new Integer[10]);

And you would get a list with 10 null elements.

Similarly,

Integer[] numbers = new Integer[10];
Arrays.fill(numbers,0);
List<Integer> list = Arrays.asList(numbers);

And you get a list with 10 elements initialised to 0;

Comments

4

The reason of null and 0 is difference between primitive and Object types. Their default values are null and 0(only false for boolean) correspondingly. See The Java Tutorial

ArrayList is not physically empty after creating. It has initial capacity, but it shows his size 0 even you initialized it exactly.Because Collection size() has logical meaning, not phisical.

List<Integer> list = new ArrayList<Integer>(10);

Also you can fill new Collection (List) with the following static method

Collections.fill(list, 0) // int 0 is auto-boxed to `Integers` 0 

1 Comment

NOTE: fill() does not work directly after initializing since the list size = 0. See nCopies() answer for this case.
3

I suppose you are looking for Collections.nCopies:

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

Comments

3

As an ArrayList doesn't initially contain any elements, it can't default the value of those elements to null or any other value.

Comments

2

The purpose of the ArrayList collection is to be virtually as fast as an array, without any waste (= preallocation) of space. Therefore, it is initialized to size 0 (i.e., empty), as opposed to having multiple null values (although practically speaking the underlying array is constructed to the specified capacity).

Having said that, note also that an ArrayList will store objects and not primitives (e.g., String objects and not char or int primitives).

Comments

1

This is because there is no constructor in ArrayList which could create an instance of certain size. Collections.nCopies(int n, T o) allows to create a List with predefined size and initialize it with any values. BTW internally it uses CopiesList class which has CopiesList(int n, E e) constructor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.