-1

This is a very basic example and I only used the existent methods.

import java.util.ArrayList;

public class A {

    public static void main(String[] args) {
        ArrayList<Integer> al = new ArrayList<Integer>();
        // This should create an ArrayList of initial capacity 10
        al.add(3,5); // Add 5 at index 3
        al.add(7,2); // Add 2 at index 7
        al.add(9,6); // Add 6 at index 9
        System.out.println(al);
    }
}

However, it throws the following exception:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 0

I don't know why the exception is thrown. It look perfectly legal to me.

Boyyett
  • 93
  • 1
  • 1
  • 5
  • 1
    You can't add at arbitrary points. Only up to the end of the array http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int,%20E) – Carlos Bribiescas Mar 02 '15 at 18:35
  • It won't work even if I do `trimToSize()` – Boyyett Mar 02 '15 at 18:36
  • I guess the [List#add() API](http://docs.oracle.com/javase/8/docs/api/java/util/List.html#add-int-E-) should give enough hint.. – Rohit Jain Mar 02 '15 at 18:36
  • Then what is the use of the overloaded add method that takes two parameters as arguments. Why is it even there if we can't use it? – Boyyett Mar 02 '15 at 18:36
  • @Boyyett That method is for adding an element in the middle of a list. – Rohit Jain Mar 02 '15 at 18:38
  • @Boyyett We can use it. It's pretty thoroughly documented as well, I think you should take some time to read the `List` API documentation and it'll answer your question. – biziclop Mar 02 '15 at 18:38
  • @Boyyett it will be important to read the documentation carefully, not just guess how various methods might work. – Eric Wilson Mar 02 '15 at 18:39
  • 2
    `add(int i, T e)` is more like a _insert at position_ function. – T.Gounelle Mar 02 '15 at 18:40
  • I know it's an inbuilt class but what exactly is an API? – Boyyett Mar 02 '15 at 18:42
  • As a general advice, I know how daunting starting a new language is, and when things in an API don't seem to make sense, our first reaction is usually "this API doesn't make sense". This is natural but it's better to train yourself to channel that frustration into trying to figure out why it works that way. (This rule doesn't always work, there are some seriously flawed APIs, even in the Java core libraries, but I found it helps a lot in the beginning.) – biziclop Mar 02 '15 at 18:44
  • Ok, how about the overloaded remove function? If we are working with integers, how does it know whether we want to remove that integer with that value or integer at that index? – Boyyett Mar 02 '15 at 18:53

5 Answers5

2

You can't add a value at index 3 to an empty list. The capacity of an array list is the size of the underlying array; the indexes still have to be within the range of items actually added to the arraylist.

yole
  • 92,896
  • 20
  • 260
  • 197
  • Ok, then how do they expect us to use the add method with two arguments? Like only to overwrite the existing values? – Boyyett Mar 02 '15 at 18:37
  • add() with two arguments does not overwrite the existing values. It can be used to add values before other values already in the list. – yole Mar 02 '15 at 18:39
  • Ok, how about the overloaded remove function? If we are working with integers, how does it know whether we want to remove that integer with that value or integer at that index? – Boyyett Mar 02 '15 at 18:54
  • See http://stackoverflow.com/questions/4534146/properly-removing-an-integer-from-a-listinteger for an answer to this. – yole Mar 02 '15 at 18:57
  • OK, I'll copy/paste it for you: list.remove(1) removes the object at position 1 and remove(new Integer(1)) removes the first occurrence of the specified element from this list. – yole Mar 02 '15 at 19:05
1

The relevant documentation is here

Note that it specifies an exception thrown if the index is greater than the size of the array ... which is different from the capacity.

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
  • Ok, how about the overloaded remove function? If we are working with integers, how does it know whether we want to remove that integer with that value or integer at that index? – Boyyett Mar 02 '15 at 18:54
  • Huh, you got me there. Maybe you should ask another question. – Eric Wilson Mar 02 '15 at 18:56
  • Ok, I just didn't want to spam by starting different threads on the same topic. – Boyyett Mar 02 '15 at 18:58
  • I understand. It's just that questions asked in comments don't get much visability, so should only be used when requesting (or offering) clarification on someone elses question/answer. – Eric Wilson Mar 02 '15 at 19:00
  • Fair enough Eric. Thanks buddy. :) – Boyyett Mar 02 '15 at 19:17
0

The capacity does not mean there are 10 empty slots which you can assign your values to. This is not how ArrayList is supposed to work. You can put your values to the list only if the index is between 0 and list.size-1, this is why you are getting the exception.

Lamorak
  • 10,957
  • 9
  • 43
  • 57
  • Ok, how about the overloaded remove function? If we are working with integers, how does it know whether we want to remove that integer with that value or integer at that index? – Boyyett Mar 02 '15 at 18:54
  • It would remove the element at int position or it would remove the element with Integer value. There is difference. – Lamorak Mar 02 '15 at 18:59
0

Your code would try to find the element at index = 3, so that it could move it to next position. However, it can't find it as there is no element there.

Tachyon
  • 452
  • 7
  • 19
0

See official docs for add(int index, E element) it throws java.lang.IndexOutOfBoundsException: if the index is out of range (index < 0 || index > size())

Check that does your case satisfies conditions or not !!!

This will work

    ArrayList<Integer> al = new ArrayList<Integer>();
    // This should create an ArrayList of initial capacity 10
    al.add(0,5); // Add 5 at index 0
    al.add(1,2); // Add 2 at index 1

    System.out.println(al);

Your case size() is 0. So you cannot put directly at al.add(3,5)

aberry
  • 447
  • 3
  • 10