Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By Fubarable
  • 2 Post By JosAH

Thread: array List help please!

  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default array List help please!

    Hey,

    Could someone please tell me why does this code gives me an index out of bounds exception?

    * cardstack is an array list of type card with 52 elements.
    * I am trying to assign a random array list object to to card
    * card is an object of type Card

    Java Code:
     randomArraylistPosition = (int)(Math.random()*51);
          
      card = cardStack.remove(randomArraylistPosition);
    I hope this makes sense
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: array List help please!

    If you call that method once, you may be OK, since you 52 Cards to start with in the collection, but if you call it more than once, then there's a chance that your randomArrayListPosition index will be a number that is greater than the size of the cardStack collection, since you are removing Cards from the collection.

    A better solution is to use the ArrayList's size in calculating the bounds of your index:

    Java Code:
    int randomIndex = (int)(Math.random() * (cardStack.size() - 1));
    Even better is to use the Random class:
    Java Code:
    // in the declaration section of your class
    private Random random = new Random();
    
    //.... later in  your program
    
    int randomIndex = random.nextInt(cardStack.size());
    and even better still is to shuffle your ArrayList to randomize its order, and simply remove the bottom Card:

    Java Code:
    // after filling your cardStack, shuffle it:
    java.util.Collections.shuffle(cardStack);
    
    // later in the code
    if (cardStack.size() > 0) {
       Card randomCard = cardStack.remove(0);
    }

  3. #3
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: array List help please!

    Quote Originally Posted by Fubarable View Post
    If you call that method once, you may be OK, since you 52 Cards to start with in the collection, but if you call it more than once, then there's a chance that your randomArrayListPosition index will be a number that is greater than the size of the cardStack collection, since you are removing Cards from the collection.

    A better solution is to use the ArrayList's size in calculating the bounds of your index:

    Java Code:
    int randomIndex = (int)(Math.random() * (cardStack.size() - 1));
    Even better is to use the Random class:
    Java Code:
    // in the declaration section of your class
    private Random random = new Random();
    
    //.... later in  your program
    
    int randomIndex = random.nextInt(cardStack.size());
    and even better still is to shuffle your ArrayList to randomize its order, and simply remove the bottom Card:

    Java Code:
    // after filling your cardStack, shuffle it:
    java.util.Collections.shuffle(cardStack);
    
    // later in the code
    if (cardStack.size() > 0) {
       Card randomCard = cardStack.remove(0);
    }
    Damn how stupid am I!!! thank you

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: array List help please!

    You're welcome. The key point of all above is to avoid using "magic numbers" such as 51. Better to let the collection or array tell you how big it is and use that number.
    awinston likes this.

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: array List help please!

    Quote Originally Posted by Fubarable View Post
    You're welcome. The key point of all above is to avoid using "magic numbers" such as 51. Better to let the collection or array tell you how big it is and use that number.
    The only magic number that should be used in Java is 0xCAFEBABE
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: array List help please!

    Quote Originally Posted by awinston View Post
    The only magic number that should be used in Java is 0xCAFEBABE
    No; the only magic numbers allowed are 0x657f5d5f and 0x2, because they're primes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: array List help please!

    Quote Originally Posted by JosAH View Post
    No; the only magic numbers allowed are 0x657f5d5f and 0x2, because they're primes.

    kind regards,

    Jos
    Sorry Jos, I don't quite understand what you mean.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: array List help please!

    Quote Originally Posted by awinston View Post
    Sorry Jos, I don't quite understand what you mean.
    Multiply those two numbers and see ...

    kind regards,

    Jos
    awinston and DarrylBurke like this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Array List
    By turtlish in forum New To Java
    Replies: 10
    Last Post: 12-20-2011, 08:48 PM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Array List help
    By Weejee37 in forum New To Java
    Replies: 4
    Last Post: 10-27-2009, 12:32 AM
  5. Array List
    By mprentice84 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 02:12 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
  •