1

This is my current progress on a project. I am trying to implement this ArrayList stuff but file continues to trow the same exception.

import java.util.*;

   public class Numerican{

      public static void main( String [] args ){

        Scanner input = new Scanner(System.in);

        ArrayList<Integer> array = new ArrayList<Integer>(10);

        int count = input.nextInt() * 2;

        while (count > 0){
           array.add( 0 );
           count = count - 1;
           array.add(count, 2);
        }

        array.add(2, input.nextInt());
        System.out.println(array.get(2));

      }
   }

It was my understanding that = new ArrayList<Integer>(10); would set the array size to 10. Did I do something wrong?

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
Alexander
  • 480
  • 2
  • 5
  • 21

6 Answers6

7
= new ArrayList<Integer>(10);

This line initializes the CAPACITY to 10, meaning the memory is allocated in the backend, but as far as you are concerned, the array is still empty.

Javadoc -

public ArrayList(int initialCapacity)
    Constructs an empty list with the specified initial capacity.

This is why the calls to add might fail, if you try to add beyond the size of the ArrayList.

p.s. remember that add function takes index first and then element, when using the 2 parameter variant.

Edit:

ArrayList has 2 different member variables, size and capacity. Capacity is how much memory is allocated, size is how many elements are inserted by programmer.

Here, Capacity = 10, Size = 0;

Karthik T
  • 29,587
  • 4
  • 58
  • 84
0

if the index is out of range (index < 0 || index > size()) IndexOutOfBoundsException will be thrown.
So i think you are accessing index > size() of the list.

size() ===> Returns the number of elements in this list.

array.add(2, input.nextInt()); here is the possible exception when your list size is 1...

Kanagavelu Sugumar
  • 16,614
  • 19
  • 77
  • 92
0

According to the javadocs:

Constructs an empty list with the specified initial capacity.

Note that the ArrayList is empty (i.e. does not contain any items). The value indicates the capacity of the ArrayList which is the number of elements which can be added before more memory must be allocated.

On the other hand, calling add() actually adds an item to the ArrayList. In your example, array.add( 0 ); adds a 0 at the end of the list and array.add(count, 2); adds a 2 at index count. I suspect the problem is that count is not a valid index in your ArrayList. You should check what its value is by inserting an SOP or using a debugger.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
0

Hey seems like your Problem is because of the line

array.add(count, 2); 

adds a 2 at index count

For example your input size is 5 then array.add(9,2); the array size is only 1 by that time as capacity and size are two different parameters for a ArrayList. So you can use a for loop instead of while to insert your values

for(int i=0; i<count;i++)
{
array.add(i,2);
}
Lakshmi
  • 1,894
  • 2
  • 23
  • 44
0

count maybe >= 10, maybe souce code can answer you question:

public void add(int index, E element) {
    rangeCheckForAdd(index);

    ensureCapacityInternal(size + 1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
                     size - index);
    elementData[index] = element;
    size++;
}

rangeCheckForAdd():

/**
 * A version of rangeCheck used by add and addAll.
 */
private void rangeCheckForAdd(int index) {
    if (index > size || index < 0)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
lichengwu
  • 4,027
  • 6
  • 24
  • 41
0

From my understanding of ArrayList, you need to add items to the list in a sequential index.
i.e. You cannot add an item to the 7th index position if 1 to 6 have not been filled in.

ArrayList.add(indexPosition, element);

If you add elements to the list, starting at indexPosition 0, and increasing the indexPosition by 1 each time, it should work.

Ex.
int i = 0;
(while i < 10){
array.add(i, numberToAdd);
i++;
}

D. Gibbs
  • 470
  • 3
  • 17