Results 1 to 9 of 9
Thread: IndexOutOfBoundsException
- 05-23-2010, 11:14 PM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
IndexOutOfBoundsException
I appreciate there are a lot of problems with this error, particularly amongst newbies, but this one really is confusing me.
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:public class StockManager { private ArrayList<String> database = new ArrayList<String>(10000);
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.Java Code:database.add(product.id, product.details);
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.
- 05-24-2010, 12:09 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 05-24-2010, 12:12 AM #3
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
If that's the case, is it possible to change the size of the ArrayList, as opposed to the capacity?
- 05-24-2010, 12:15 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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>.
- 05-24-2010, 12:19 AM #5
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Thanks for your help curmudgeon. I think I can find a suitable workaround from those options.
- 05-24-2010, 12:20 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Cool!
¡Que tenga mucha suerte!
- 05-24-2010, 12:45 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 32
- Rep Power
- 0
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:
Then I add a object with index 2 before index 1 --> errorJava Code:ArrayList array = new ArrayList<String> (10);
Or if I add object 0 and 2 (not add object 1), the same error:Java Code:array.add(0, "werwe"); array.add(2, "asdfad"); array.add(1, "weroiu");
This code must be change to:Java Code:array.add(0, "qweqw"); array.add(2, "safd");
The index must be continues: 0 --> 9 (in my case, and in your case: 0 --> 9999).Java Code:array.add(0, "sasda"); array.add(1, "werw"); array.add(2, "asdfsdf"); ...
In your case, you can use Map, it stores data by hashtable type: key --> value
Sorry for my bad English,
banhbaochayLast edited by banhbaochay; 05-24-2010 at 12:48 AM.
- 05-24-2010, 01:37 AM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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");
Your English is fine. :)Sorry for my bad English,
banhbaochay
- 05-24-2010, 03:44 AM #9
Member
- Join Date
- Apr 2010
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
IndexOutOfBoundsException with AWT-EventQueue-0
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 03-10-2009, 01:52 PM -
IndexOutOfBoundsException
By aldo1987 in forum New To JavaReplies: 6Last Post: 04-30-2008, 03:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks