英文:
Java : Populate arraylist with for loop
问题
目前我正在尝试通过使用 for 循环来填充一个 ArrayList,以获取每张卡片来创建一副牌组。但它并不像我预期的那样工作,希望有人能指点我正确的方向。
我看到了以下错误:
System.ArgumentOutOfRangeException: 值必须大于或等于零且小于该维度上控制台缓冲区的大小。
我的牌组类:
```java
public class Deck{
private final int deckSize = 52;
private final String[] suit = {"hearts", "clubs", "diamonds", "spades"};
private final String[] face = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
ArrayList<String> currentDeck = new ArrayList<String>(deckSize);
public void getDeck(){
for(int i=0; i<face.length; i++) {
for(int j=0; j<suit.length; j++){
String cards = face[i] + " of " + suit[j];
currentDeck.add(cards);
}
}
System.out.println(currentDeck);
}
}
主类:
public static void main(String[] args) {
printDeck();
}
public static void printDeck(){
Deck deck = new Deck();
deck.getDeck();
}
英文:
Currently I am trying to populate an ArrayList with a deck of cards,through the use of a for loop to get each card. It isn't quite working like I intended and was hoping for someone to point me in the right direction.
I'm seeing the following error:
System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
My Deck Class:
public class Deck{
private final int deckSize = 52;
private final String[] suit = {"hearts", "clubs", "diamonds", "spades"};
private final String[] face = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
ArrayList<String> currentDeck = new ArrayList<String>(deckSize);
public void getDeck(){
for(int i=0; i<face.length; i++) {
for(int j=0; j<suit.length; j++){
String cards = face[i] + " of " + suit[j];
currentDeck.add(cards);
}
}
System.out.println(currentDeck);
}
}
Main Class;
public static void main(String[] args) {
printDeck();
}
public static void printDeck(){
Deck deck = new Deck();
deck.getDeck();
}
</details>
# 答案1
**得分**: 0
```java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Deck deck = new Deck();
deck.getDeck();
}
}
class Deck {
private final int deckSize = 52;
private final String[] suit = {"hearts", "clubs", "diamonds", "spades"};
private final String[] face = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
ArrayList<String> currentDeck = new ArrayList<String>(deckSize);
public void getDeck() {
for (int i = 0; i < face.length; i++) {
for (int j = 0; j < suit.length; j++) {
String cards = face[i] + " of " + suit[j];
currentDeck.add(cards);
}
}
System.out.println(currentDeck);
}
}
Output:
[1 of hearts, 1 of clubs, 1 of diamonds, 1 of spades, 2 of hearts, 2 of clubs, 2 of diamonds, 2 of spades, 3 of hearts, 3 of clubs, 3 of diamonds, 3 of spades, 4 of hearts, 4 of clubs, 4 of diamonds, 4 of spades, 5 of hearts, 5 of clubs, 5 of diamonds, 5 of spades, 6 of hearts, 6 of clubs, 6 of diamonds, 6 of spades, 7 of hearts, 7 of clubs, 7 of diamonds, 7 of spades, 8 of hearts, 8 of clubs, 8 of diamonds, 8 of spades, 9 of hearts, 9 of clubs, 9 of diamonds, 9 of spades, 10 of hearts, 10 of clubs, 10 of diamonds, 10 of spades, Jack of hearts, Jack of clubs, Jack of diamonds, Jack of spades, Queen of hearts, Queen of clubs, Queen of diamonds, Queen of spades, King of hearts, King of clubs, King of diamonds, King of spades]
英文:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Deck deck = new Deck();
deck.getDeck();
}
}
class Deck {
private final int deckSize = 52;
private final String[] suit = {"hearts", "clubs", "diamonds", "spades"};
private final String[] face = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
ArrayList<String> currentDeck = new ArrayList<String>(deckSize);
public void getDeck() {
for (int i = 0; i < face.length; i++) {
for (int j = 0; j < suit.length; j++) {
String cards = face[i] + " of " + suit[j];
currentDeck.add(cards);
}
}
System.out.println(currentDeck);
}
}
I have copied your code only and this code works perfectly fine. I have just added the main method and called your method. And, the output that i am getting is
[1 of hearts, 1 of clubs, 1 of diamonds, 1 of spades, 2 of hearts, 2 of clubs, 2 of diamonds, 2 of spades, 3 of hearts, 3 of clubs, 3 of diamonds, 3 of spades, 4 of hearts, 4 of clubs, 4 of diamonds, 4 of spades, 5 of hearts, 5 of clubs, 5 of diamonds, 5 of spades, 6 of hearts, 6 of clubs, 6 of diamonds, 6 of spades, 7 of hearts, 7 of clubs, 7 of diamonds, 7 of spades, 8 of hearts, 8 of clubs, 8 of diamonds, 8 of spades, 9 of hearts, 9 of clubs, 9 of diamonds, 9 of spades, 10 of hearts, 10 of clubs, 10 of diamonds, 10 of spades, Jack of hearts, Jack of clubs, Jack of diamonds, Jack of spades, Queen of hearts, Queen of clubs, Queen of diamonds, Queen of spades, King of hearts, King of clubs, King of diamonds, King of spades]
I believe this is what you want.
答案2
得分: 0
问题现已解决。以下是代码,适用于遇到相同问题的任何人!
class Deck {
private final int deckSize = 52;
private final String[] suit = {"红心", "方块", "梅花", "黑桃"};
private final String[] face = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
ArrayList<String> currentDeck = new ArrayList<String>(deckSize);
public void getDeck() {
for (int i = 0; i < face.length; i++) {
for (int j = 0; j < suit.length; j++) {
if(j == 3){
String card = face[i] + " " + suit[j];
currentDeck.add(card + "\n");
}
else{
String cards = face[i] + " " + suit[j];
currentDeck.add(cards);
}
}
}
System.out.println(currentDeck);
}
}
英文:
Problem now resolved. Here is code for anyone having same problem!
class Deck {
private final int deckSize = 52;
private final String[] suit = {"hearts","diamonds", "clubs", "spades"};
private final String[] face = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
ArrayList<String> currentDeck = new ArrayList<String>(deckSize);
public void getDeck() {
for (int i = 0; i < face.length; i++) {
for (int j = 0; j < suit.length; j++) {
if(j == 3){
String card = face[i] + " of " + suit[j];
currentDeck.add(card + "\n");
}
else{
String cards = face[i] + " of " + suit[j];
currentDeck.add(cards);
}
}
}
System.out.println(currentDeck);
答案3
得分: 0
这不是一个代码问题,而是环境默认配置值的结果。在Windows Server下,您可以更改控制台的默认屏幕缓冲区大小以避免此问题。
英文:
This is not a code problem, but a result of the value of the environment's default configuration. Under Windows Server, you can change the default screen buffer size of your console to avoid it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论