Results 1 to 3 of 3
- 05-11-2011, 03:45 PM #1
Member
- Join Date
- Apr 2011
- Location
- Glasgow
- Posts
- 5
- Rep Power
- 0
Adding an item to arrayList at a specific index
Hi, Im trying to add an element to a specific index in an ArrayList
I know for an arrayList if you want to add an element to a specific place you do write arraylist.add(1, 5) to add 5 to position 1, but do you know how to put a variable name in instead of a number.
I tried arrayList.add(count, 5) where count equals an integer but I keep getting an out of bounds exception
Count will constantly change and i need to add the elements to the index which equals it.
Any help would be greatly appreciated
Thank you very much :)
- 05-11-2011, 04:15 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
ArrayList.add
->
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
If the size of your ArrayList is lower than count, you will get this exception!
Example: You can not invoke add(3,1337) if your ArrayList has only one element in it! (Index: 3, Size: 1)
Btw: size = the number of elements in this list (not the initialCapacity!!!!)
- 05-11-2011, 04:25 PM #3
Member
- Join Date
- Apr 2011
- Location
- Glasgow
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Help required with complex method: adding item
By nignogs in forum New To JavaReplies: 14Last Post: 05-11-2011, 01:06 PM -
get specific arrayList from collection
By computerbum in forum New To JavaReplies: 1Last Post: 03-23-2011, 04:11 AM -
NullPointer adding an item to a LinkedList
By sehudson in forum New To JavaReplies: 7Last Post: 03-10-2011, 03:39 AM -
Inserting a SWT tree item at an index
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:55 PM -
grabbing an item from an arraylist.
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-17-2008, 08:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks