Greg Peckory
Greg Peckory

Reputation: 8058

Appending indexes to multidimensional arrays in java

public class NewDeck {

    public static void main(String[] args){

        String[] suits = new String[] {"Clubs", "Diamonds", "Spades", "Hearts"};

        String[] faces = new String[] {"Ace", "King", "Queen", "Jack"};

        String[][] deck = new String[][] {{},{}};

        int a = 0;
        int b = 0;

        for(String x: suits){

            a ++;

            for(String y: faces){


                deck[a][b] = {{x, y}};  // THIS IS THE LINE I NEED HELP WITH

                b++;

        System.out.println(deck);

            }

        }

    }

}

What I expect to print is:

{{"Clubs, "Ace"}{"Diamonds", "Ace"}{"Spades", "Ace"}{"Hearts", "Ace"}
 {"Clubs, "King"}{"Diamonds", "King"}{"Spades", "King"}{"Hearts", "King"}
 {"Clubs, "Queen"}{"Diamonds", "Queen"}{"Spades", "Queen"}{"Hearts", "Queen"}
 {"Clubs, "Jack"}{"Diamonds", "Jack"}{"Spades", "Jack"}{"Hearts", "Jack"}

And when I want to print:

deck[4][0]

I expect it to print:

"Clubs"

I know I'm very close it is only one line I believe I'm doing wrong, so I commented on the line that's causing the problem. If anyone knows what to put in instead of:

deck[a][b] = {{x, y}};

It would be most appreciated. Thanks for any answers in advance

Upvotes: 0

Views: 69

Answers (3)

MAnyKey
MAnyKey

Reputation: 567

I think you will need some Card class for it.

public class NewDeck {
  public static class Card {
    public final String suit;
    public final String face;
    public Card(String suit, String face) {
      this.suit = suit;
      this.face = face;
    }
    public String toString() {
      return "{\"" + suit + "\", \"" + face + "\"}";
    }
  }
  public static void main(String[] args) {
    String[] suits = new String[] {"Clubs", "Diamonds", "Spades", "Hearts"};
    String[] faces = new String[] {"Ace", "King", "Queen", "Jack"};

    Card[][] deck = new Card[suits.length][faces.length];

    int a = 0;
    int b = 0;
    for(String x: suits){
      b = 0;
      for(String y: faces){
        deck[a][b] = new Card(x, y);
        b++;
      }
      a++;
    }
  }
}

Upvotes: 0

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

deck[a][b] = {{x, y}};

Replace this with something like this :

deck[index][0]=x;
deck[index][1]=y;

Also deck[4][0] wont yield you correct results. Remember array has elements indexed starting at 0 and ending at array-size-1.

System.out.println(deck); You will not be able to print values of the array by simply printing the array itself.
The above statement will simple print the hashcode of the array object. Remember it is an array not a List or a Set. You need to iterate over the array and print the elements within it.

Upvotes: 1

PoByBolek
PoByBolek

Reputation: 3905

public static void main(String args[]) {
    String suits[] = new String[] {"Clubs", "Diamonds", "Spades", "Hearts"};
    String faces[] = new String[] {"Ace", "King", "Queen", "Jack"};
    String deck[][] = new String[suits.length * faces.length][2];

    int i=0;

    for (String suit : suits) {
        for (String face : faces) {
            deck[i][0] = suit;
            deck[i][1] = face;
            i++;
        }
    }
}

Upvotes: 2

Related Questions