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
286k25 gold badges267 silver badges391 bronze badges
asked Feb 22, 2013 at 5:13
7
  • What line causes the error? Commented Feb 22, 2013 at 5:17
  • 2
    I suppose problem is with this line array.add(count, 2); . Is it? Commented Feb 22, 2013 at 5:17
  • @codeMan That's my guess, too, after a closer look. Commented Feb 22, 2013 at 5:23
  • 1
    I dont think so. I tried removing the while loop and it still gave me the same exception. Commented Feb 22, 2013 at 5:25
  • @Alexander array.add(2, input.nextInt()); here is the possible exception... when count is 1. you are accessing more than the size of the list which is 2. Commented Feb 22, 2013 at 5:26

6 Answers 6

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;

answered Feb 22, 2013 at 5:21

8 Comments

I guess I still don't understand why the while loop would not add to the array. Even when the count is less than 10 (array size)
@Alexander you need to understand how ArrayList works. It has 2 members, size and capacity. Capacity is how much memory is allocated, size is how many elements are inserted by programmer
Here the array CAPACITY is 10, and as javadoc reveals, initial size is 0
So, can you shed some light on a possible solution?
@Alexander is it not possible to add without specific index? If not you can write a loop that will add 10 elements before this code.
|
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.

answered Feb 22, 2013 at 5:22

Comments

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));
}
answered Feb 22, 2013 at 5:35

Comments

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...

answered Feb 22, 2013 at 5:21

2 Comments

@asifsid88 I have metioned the exact line and the cause of the issue too... what else you want ??
@asifsid88 I didn't get your point; without that while loop also he is able to see the exception rit ??
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++;
}

answered Feb 22, 2013 at 5:46

Comments

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);
}
answered Feb 22, 2013 at 5:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.