0

I'm currently working on Java Collection Framework , and created a list of size 10 still the program prompting me that the list size is 0, further if list processed it throws IndexOfBoundException of course

I've searched about this but I don't know why it's happening to me

    int size =sc.nextInt();
    List<Integer> list = new ArrayList<Integer>(size);   
    System.out.println(list+" "+list.size());

I expect the output as [0,0,0,0,0,0,0,0,0,0] 10 but output is [] 0 .

3
  • 2
    you are creating ArrayList with initial capacity not size Commented Jul 18, 2019 at 5:34
  • The whole point of using a list over an array is the dynamic size, why would you want to define size of list ? Commented Jul 18, 2019 at 5:35
  • Stop, document time ... (sorry, it's a long day) - ArrayList(int) does not "fill" the underlying buffer, it simple allocates the size buffer - ArrayList is backed by an array, so it takes time to resize, providing a initial buffer can help performance Commented Jul 18, 2019 at 5:36

4 Answers 4

1

To initialize the List to a given size with default value, you can use nCopies:

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

Now your println statement will output:

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

(assuming size is 10).

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

2 Comments

We can also use like this List<Integer> list = Collections.nCopies(size, 0);
@ashishpawar Not if you want to modify that list later, since nCopies returns an immutable List.
1

Look at the documentation, it says empty list is created with specified capacity.

public ArrayList(int initialCapacity)

Constructs an empty list with the specified initial capacity.

What is this capacity and why is it useful?

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

1 Comment

yes until you add elements to list it will be empty, if you are fixed about size then go for Array so you will get what you want @ashishpawar
0

The parameter initializes an internal array of a particular capacity (the default is actually already 10, if you give no argument)

You must loop to fill up the list with values, and the outer collection of your list starts at size 0

5 Comments

I've used ` Collections.fill(list,0);` but still same output
You can't fill an empty collection. You need to manually add elements. You could add null 10 times, then call fill, for example
If you really want a fixed array, then you want new int[size]
I need to specify initial size that might grow as program further executed but I’ve problem that even accessed first element it says indexOutOfBound exception ?
Because you didn't add anything yet. There is no initial size for collection subclasses. And like I said, 10 is already the default, and it will grow as you add more than half that size
0
 List<Integer> list = new ArrayList<Integer>()
    {   
        {
             for (int i = 0; i < size; i++) {
                 add(0);
             }   
        }
    };
    System.out.println(list+" "+list.size());

Now we can have proper size of List and initialised with 0.

1 Comment

List<Integer> list = Collections.nCopies(size, 0); for shorthand

Your Answer

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