0

Good day!

I am wondering How can I access an array index of an Object instance.

My code is as follows:

public class PokerPlayer {

    private String name = null;
    private Card [] cardsOnHand = new Card[5]; 

    //getter and setter
}

What i want is to access the cardsOnHandArray[index] so that i can call it on another class and set the values per index...

public class PokerGame {
      public static void main (String [] Args){
      PokerPlayer players []= new PokerPlayer[4];
      for(PokerPlayer player : players){
          for(int i =0; i<5; i++){
            //ACCESS cardsOnHand index i and store something on it...
          }
        }
    }
}

Any advice would be highly appreciated. How can i also improve my OO design? Thank you in advance

1
  • You should really avoid magic numbers, maybe get rid of 'i<5' and get the value from the Card array, i.e 'i<player.getCardsOnHand.length'. Commented May 5, 2011 at 15:56

6 Answers 6

4
public class PokerPlayer {
...
public Card getCard(int index) {
  return this.cardsOnHand[index];
}

public void setCard(int index, Card card) {
  this.cardsOnHand[index] = card;
}
...
}

then use:

player.getCard(i);
player.setCard(i,new Card());
Sign up to request clarification or add additional context in comments.

Comments

3

You almost have it. Just call this inside your inner loop:

cardsOnHand[i] = new Card();

Of course, change what is being assigned to the array according to your requirements.

2 Comments

but i want to store something in the array cardsOnHand [5]
There is no such thing as cardsOnHand[5] if the length of the array is 5. The index values would be 0, 1, 2, 3, 4.
1

you can do as:

for(PokerPlayer player : players){
          for(int i =0; i<5; i++){
            Card[] cards= player[i].getCardsOnHand();
            cards[i] = new Card();
          }
        }

Comments

1

This should work:

public class PokerGame {
  public static void main (String [] Args){
  PokerPlayer players []= new PokerPlayer[4];
  for(PokerPlayer player : players){
      for(int i =0; i<5; i++){
        Card card = player.cardsOnHand[i];
      }
    }
}

}

Comments

1

Since your cardsOnHand array is private, you have to use your (unprovided) setter function. Which could be something like

public void setCard(int index, Card value){
   cardsOnHand[index] = value;
}

And in used in your loop as
player.setCard(i, something)

Comments

1

Assuming that your PokerPlayer has a getter for the cardsOnHand array:

public class PokerGame {
      public static void main (String [] Args){
      PokerPlayer players []= new PokerPlayer[4];
      for(PokerPlayer player : players){
          for(int i =0; i<5; i++){
                player.getCardsOnHand()[i] = new Card();
          }
        }
    }
}

However, I think that a better solution is to add a method

public void setCard(Card card, int index) {
    assert index < 5;
    cardOnHands[index] = card
}

to your PokerPlayer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.