Results 1 to 9 of 9
  1. #1
    Luciform is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Question IndexOutOfBoundsException

    I appreciate there are a lot of problems with this error, particularly amongst newbies, but this one really is confusing me.

    Java Code:
    public class StockManager
    {
        private ArrayList<String> database = new ArrayList<String>(10000);
    The above is intended to create a new ArrayList with 10,000 indices. In a method later on the following statement attempts to execute:

    Java Code:
    database.add(product.id, product.details);
    product.id is an integer, and product.details is a string. If my understanding of an ArrayList's add method is correct, the two parameters determine the index of the ArrayList element that data is to be added to, and then the data to be added.

    When the above statement is reached in the method call, I get the error "IndexOutOfBoundsException: Index: 3055, Size: 0 (in java.util.ArrayList)" (product.id is equal to 3055).

    What I don't understand is how can index 3055 be out of bounds when the ArrayList database has indices 0 to 9999? (I have double-checked that this is the case using the Object Inspector in my IDE.) If anyone can offer even the most painfully obvious fixes, I'd be very grateful.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Constructing an ArrayList with a 1000 int parameter doesn't add 1000 items to the ArrayList, but rather gives the ArrayList an "initial capacity" of 1000 items. that is, this capacity is the size of the array that is held within the ArrayList object and that holds the items of the ArrayList. But this array works behind the scenes, and so giving the arraylist 1000 really just optimizes it for holding 1000 or slightly less items, nothing more. When your ArrayList starts out, even if it has a capacity of 1000, its size is still 0.

    As an aside, wouldn't you be better off creating an ArrayList<Product> rather than one of String? This way you can simply add Products to the ArrayList at whatever position with the add(Object o) method.

  3. #3
    Luciform is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    If that's the case, is it possible to change the size of the ArrayList, as opposed to the capacity?

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Luciform View Post
    If that's the case, is it possible to change the size of the ArrayList, as opposed to the capacity?
    Yes, by adding items to the ArrayList, but this begs the question of why? Again, I suspect that you're better off creating an ArrayList<Product> and just adding Product objects in without care for position. But if you want to add items to a collection in a sparse manner with the key or index being an int, consider using a HashMap<Integer, String> or HashMap<Integer, Product>.

  5. #5
    Luciform is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your help curmudgeon. I think I can find a suitable workaround from those options.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Cool!

    ¡Que tenga mucha suerte!

  7. #7
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    Hi, Luciform,
    AFAIK, your constructor ArrayList and add method are correct. The problem maybe in your product.id, is it continous? You can check it by printing to output all of product.id.
    NOTE: ArrayList only can add object with continuous index. For example: I have this ArrayList as follow:
    Java Code:
    ArrayList array = new ArrayList<String> (10);
    Then I add a object with index 2 before index 1 --> error
    Java Code:
    array.add(0, "werwe");
    array.add(2, "asdfad");
    array.add(1, "weroiu");
    Or if I add object 0 and 2 (not add object 1), the same error:
    Java Code:
    array.add(0, "qweqw");
    array.add(2, "safd");
    This code must be change to:
    Java Code:
    array.add(0, "sasda");
    array.add(1, "werw");
    array.add(2, "asdfsdf");
    ...
    The index must be continues: 0 --> 9 (in my case, and in your case: 0 --> 9999).
    In your case, you can use Map, it stores data by hashtable type: key --> value

    Sorry for my bad English,
    banhbaochay
    Last edited by banhbaochay; 05-24-2010 at 12:48 AM.

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by banhbaochay View Post
    NOTE: ArrayList only can add object with continuous index.
    I think the requirement is that the index be between 0 and ArrayList.size(). For instance this is perfectly legal and in many situations, logical:

    Java Code:
    ArrayList array = new ArrayList<String> (10);
    array.add(0, "werwe");
    array.add(0, "asdfad");
    array.add(0, "weroiu");

    Sorry for my bad English,
    banhbaochay
    Your English is fine. :)

  9. #9
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    Hi curmudgeon,
    You're right, it's my fault :)
    Thank you very much. My experience's increasing with your information :)

Similar Threads

  1. IndexOutOfBoundsException with AWT-EventQueue-0
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 01:52 PM
  2. IndexOutOfBoundsException
    By aldo1987 in forum New To Java
    Replies: 6
    Last Post: 04-30-2008, 03:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •