0

I am having hard time understanding why the size of ArrayList is zero by default here as opposed to what I have seen on different article that it is actually 10. And even if I change the size, it doesn't get affected.

import java.util.ArrayList;
Class Test{
    private static final ArrayList<Long> foo = new ArrayList<>(1000);
   
    public static void main(String[] args) {
        System.out.println(foo.size());
}

Can anyone explain this?

  • 4
    Size != capacity – M A Jun 14 '21 at 13:40
  • `ArrayList` allocated space for ten items by default. It still has a size of zero – GBlodgett Jun 14 '21 at 13:40
  • Thanks for replying guys. So if the space is being allocated to the given ArrayList shouldn't we be able to access it. But when accessing any element within the specified size it throws IndexOutOfBoundException – DrusePstrago Jun 14 '21 at 13:50
  • Does this answer your question? [Initial size for the ArrayList](https://stackoverflow.com/questions/8896758/initial-size-for-the-arraylist) – Yolomep Jun 14 '21 at 13:56

2 Answers2

2

You have to differentiate between the data structure interface you are using, which is that of a java.util.List and its internal implementation, which is that of a java.util.ArrayList which means it is backed by an array with an initial capacity that will get recreated if its capacity is not enough to hold all the elements.

The constructor ArrayList<T>(int n) does not create a java.util.List with size n, but instead an empty java.util.List (size 0), with it's implementation java.util.ArrayList creating an internal backing array of size n.

However List#size is a method of the interface and reports the size of the list, not the size of the internal backing array, which it does not know about.

Accordingly, if you want to access element 10, you correctly get an exception, because you did not add any elements to the list yet.

If you want to initialize it with n zeroes, see How can I initialize an ArrayList with all zeroes in Java?.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • Can you list the practical use-case of specifying capacity. Like what significance it does have. And can you suggest me some other data structure that initializes all the position within the specified capacity to default value like array in java and list in python – DrusePstrago Jun 14 '21 at 13:59
  • @GauravPandey: It can make your program faster if you know beforehand how many items you want to store, so that it does not need to copy the elements to a new array every time the capacity gets too small. However whether that amount of saved time is actually significant to you depends on your use case and how often you create arrays, e.g. if you are in a loop and do this billions of times. If you are a beginner and you just create the array list once and not in a very frequently called loop, you don't need to do that. – Konrad Höffner Jun 14 '21 at 14:02
  • Thanks for answering @Konrad It was really helpful. – DrusePstrago Jun 14 '21 at 14:37
1

Size of an Array

We created an Integer array of size 1000, which resulted in the below

Integer[] a = new Integer[1000]; 
System.out.println("Size of the Array: " + a.length);

Result:

Size of the Array: 1000

Capacity of an ArrayList

We created an ArrayList of size 1000, which resulted in the below

List<Integer> a = new ArrayList<>(1000);
System.out.println("Size of the ArrayList : " + a.size());

Result:

Size of the ArrayList : 0

As no elements have been added then the size is 0.

Add an element to the ArrayList and check the size

List<Integer> a = new ArrayList<>(1000);
a.add(1);
System.out.println("Size of the ArrayList : " + a.size());

Result:

Size of the ArrayList : 1

Arrays are fixed size but ArrayList are resize size

Ihdina
  • 950
  • 6
  • 21