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);
}
}
}
}
我希望打印的是:
{{"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"}
当我要打印时:
deck[4][0]
我希望它能打印:
"Clubs"
我知道我非常接近,只有一条线我认为我做错了,所以我评论了引起问题的那条线。如果有人知道要放什么,而不是:
deck[a][b] = {{x, y}};
将不胜感激。
感谢您提前提出任何答案
最佳答案
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++;
}
}
}
关于java - 在Java中将索引附加到多维数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18031758/