Results 1 to 10 of 10
Thread: ArrayList size error
- 03-28-2011, 07:00 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
ArrayList size error
Hi, im using arraylist to store a collection of objects. I have a menu that do a lot of things, also loading/saving. Saving works well, and loading too, but im having a hard time trying to copy the loaded collection in the actual one, because i get IndexOutOfBoundsException in the collection, when calling collections.copy.
Here is the code. I translate and coment each thing:Java Code:public void cargarRacionals() { ObtenirRacional or = new ObtenirRacional(); System.out.println("XX:" + (or.retornarRacionals().size())); //racionals.ensureCapacity((or.retornarRacionals().size())); ArrayList<Racional> racionals = new ArrayList<Racional>(or.retornarRacionals().size()+1); Collections.copy(racionals, or.retornarRacionals()); }
racionals : the arraylist of racional objects.
ObtenirRacional :the class that loads and deserializes.
retornar racionals: return the array
Ok. The error is in the last line, and i dont get why. I have tried with the two ways:
//racionals.ensureCapacity((or.retornarRacionals().s ize()));
ArrayList<Racional> racionals = new ArrayList<Racional>(or.retornarRacionals().size()+ 1);
I create a new array with the needed size, or i set the new size. Note that size is ok, its says its 8 and it goes well.
pd: sorry for my bad english
-
try this instead:
racionals.AddRange(or.retornarRacionals());
- 03-28-2011, 08:21 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Array list dont have that method.
-
my bad, it should be
racionals.addAll
- 03-28-2011, 09:26 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
when you are tring to copy,A stack overflow is being occured.That means,the destinaton array-size is smaller than the [copy-from]array size.Now what to do,you know very well..
Regards,
Ujjal
-
- 03-28-2011, 09:45 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Yes, now i use a clear + add all and works fine. But i dont know why the resize option or the constructor one dont work...
thanks anyway
- 03-29-2011, 09:20 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
What was the full error you got?
Stack trace and all?
Might give us an idea of what it thought the problem was.
- 03-29-2011, 09:53 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
stack trace?
i dont remember anything strange in the errror, only all the calls like main-objecta,object-method,etc.
- 03-29-2011, 09:57 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
ArrayList size while adding or setting
By GaBuG in forum New To JavaReplies: 2Last Post: 01-02-2011, 06:31 PM -
ArrayList pointer size java
By senorbum in forum Advanced JavaReplies: 6Last Post: 05-20-2010, 09:44 AM -
ArrayList IndexOutOfBounds... error
By Arius in forum New To JavaReplies: 7Last Post: 01-17-2010, 10:15 PM -
Limiting size of ArrayList
By ravian in forum New To JavaReplies: 3Last Post: 01-29-2008, 06:37 AM -
How to reduce the size or avoiding out of memory error?
By rajeshkumarmsc in forum Advanced JavaReplies: 3Last Post: 08-11-2007, 10:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks