Help with "ArrayIndexOutOfBoundsException" on code using Vector class and methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • al666940
    New Member
    • May 2010
    • 1

    Help with "ArrayIndexOutOfBoundsException" on code using Vector class and methods

    Hello everyone,

    I'm coding card functions (shuffle, draw, etc) using the Vector class and functions, and I'm almost finished except for an exception that I can't seem to get rid of.

    Can anyone help me please? It's 2 lines of code which are causing the exception, and the problem is that I do not understand why it is throwing the exception, it should work.

    Either it's them or my FOR function containing them is somehow flawed.

    Lines are highlighted below: the last two add methods from Vector class.

    //2.- reassemble pool and remaining deck on result deck , taking one turn each to effectively shuffle
    deckSize = (deckV.size()*2 ) - 1;
    System.out.prin tln("Size of original deck is " + deckSize);
    System.out.prin tln("Size of deckV is " + deckV.size());
    System.out.prin tln("Size of deck2V is " + deck2V.size());
    System.out.prin tln("First element on deckV is " + deckV.get(0));
    System.out.prin tln("First element on deck2V is " + deck2V.get(0));
    for (int i = 0; i < deckSize; i++) {

    // EXCEPTION IS HERE!!!!!!!! ANY OF THESE TWO THROWS IT

    deck3V.add(deck V.remove(0));
    deck3V.add(deck 2V.remove(0));
    }

    // print resulting reshuffled deck
    System.out.prin tln(" ");
    System.out.prin tln("Here begins reshuffled deck");
    System.out.prin tln("Size of resulting deck is " + poolV.size());
    for (int i = 0; i < poolV.size(); i++) {
    System.out.prin tln(poolV.get(i ));
    }


    }

    }


    Exception is (from printout):

    Size of original deck is 51
    Size of deckV is 26
    Size of deck2V is 26
    First element on deckV is 8 of Hearts
    First element on deck2V is 2 of Clubs
    Exception in thread "main" java.lang.Array IndexOutOfBound sException: Array index out of range: 0
    at java.util.Vecto r.remove(Unknow n Source)
    at can.McAfee.exam ple1.Deck2.main (Deck2.java:137 )
    Last edited by al666940; May 10 '10, 12:27 AM. Reason: Removing extra code which is not relevant
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Do you have any guarantees as to the size of each of the 2 original decks?
    DeckV and Deck2V

    You'll definitely get an out of bounds error because the original first deck had size:
    deckSize = deckV.size(), not
    deckSize = (deckV.size()*2 ) - 1

    Comment

    Working...