Results 1 to 10 of 10
  1. #1
    wepwep is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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.

    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());
        }
    Here is the code. I translate and coment each thing:
    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

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    try this instead:
    racionals.AddRange(or.retornarRacionals());

  3. #3
    wepwep is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Array list dont have that method.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    my bad, it should be
    racionals.addAll

  5. #5
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    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

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    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..
    he's done this though:
    or.retornarRacionals().size()+1

    to ensure its larger. so i'm not sure where the problem is coming from but thats why i was suggesting to use .addAll(Collections c) method rather than copy, because you can add infinitely can't you?

  7. #7
    wepwep is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    What was the full error you got?
    Stack trace and all?

    Might give us an idea of what it thought the problem was.

  9. #9
    wepwep is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    stack trace?

    i dont remember anything strange in the errror, only all the calls like main-objecta,object-method,etc.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    That's the stack trace.
    And the full error text will tell you what index it is trying to use as well.

Similar Threads

  1. ArrayList size while adding or setting
    By GaBuG in forum New To Java
    Replies: 2
    Last Post: 01-02-2011, 06:31 PM
  2. ArrayList pointer size java
    By senorbum in forum Advanced Java
    Replies: 6
    Last Post: 05-20-2010, 09:44 AM
  3. ArrayList IndexOutOfBounds... error
    By Arius in forum New To Java
    Replies: 7
    Last Post: 01-17-2010, 10:15 PM
  4. Limiting size of ArrayList
    By ravian in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:37 AM
  5. How to reduce the size or avoiding out of memory error?
    By rajeshkumarmsc in forum Advanced Java
    Replies: 3
    Last Post: 08-11-2007, 10:15 PM

Posting Permissions

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