Out of Bounds

I get that it say out of bounds, Where?

Page 1 of 1

1 Replies - 418 Views - Last Post: 11 January 2011 - 09:55 AM Rate Topic: -----

#1 Luxaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-January 11

Out of Bounds

Posted 11 January 2011 - 09:48 AM

It sais in my code i get "out of bounds". I have searched some times for where and why, but didnt find.
Where did my easy code go wrong?


	private ArrayList<Integer> Hjarter = new ArrayList<Integer>();
	private ArrayList<Integer> Spader = new ArrayList<Integer>();
	private ArrayList<Integer> Klover = new ArrayList<Integer>();
	private ArrayList<Integer> Ruter = new ArrayList<Integer>();
	private ArrayList<String> Kortlek = new ArrayList<String>();


public ArrayList<String> Blanda (){
			Kortlek.clear();
			
		for (int m = 1; m<=13; m++){			
			Hjarter.add(m);
			Spader.add(m);
			Klover.add(m);
			Ruter.add(m);	
		}
		
		
		Random rnd = new Random();
				
		
		for(int i = 0; i<52; i++ ){
			int random = rnd.nextInt(13);
			int rand = rnd.nextInt(4);
			
			if (rand == 0){			
				String h = String.valueOf(Hjarter.get(random));                 //Out of bounds
				Kortlek.add(i, ("H" + h));
				Hjarter.remove(random);	
			}
			else if (rand == 1){
				String s = String.valueOf(Spader.get(random));                  //Out of bounds
				Kortlek.add(i, ("S" + s));
				Spader.remove(random);
			}
			else if (rand == 2){
				String k = String.valueOf(Klover.get(random));                  //Out of bounds
				Kortlek.add(i, ("K" + k));
				Klover.remove(random);
			}
			else if (rand == 3){
				String r = String.valueOf(Ruter.get(random));                   //Out of bounds
				Kortlek.add(i, ("R" + r));
				Ruter.remove(random);
			}
			}
		return Kortlek;
		}




BTW, code supposed to shuffle a card deck

Is This A Good Question/Topic? 0
  • +

Replies To: Out of Bounds

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Out of Bounds

Posted 11 January 2011 - 09:55 AM

First, why not create a new class for Card?
public class Card {
   //member for rank
   //member for suit
   //methods..
} 

Then you can have an ArrayList of Card objects.
init all the cards, and then you can use the shuffle method of Collections:
dont forget to
import java.util.Collections;

then,
ArrayList<Card> deck = new ArrayList<Card>();
//fill the deck...
//then call shuffle
Collections.shuffle(deck);


You can check pbl's snippet for a deck of cards:
http://www.dreaminco...snippet3176.htm
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1