【问题标题】:Java : Populate arraylist with for loopJava:用for循环填充arraylist
【发布时间】:2020-07-18 17:41:42
【问题描述】:

目前我正在尝试通过使用 for 循环来获取每张卡片,用一副卡片填充 ArrayList。它并没有像我预期的那样工作,希望有人能指出我正确的方向。

我看到以下错误:

System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.

我的甲板课:

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();
    }

【问题讨论】:

  • 你打算如何工作?
  • 出现哪个错误?逻辑工作正常。在任何情况下,您都需要声明 main 方法。请发送您的文件的可复制示例
  • 请提供预期的输出。逻辑似乎很好。
  • 我已经实例化了甲板类,并且在主方法中正确调用了该方法作为deck.getDeck。我收到错误Exception: System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
  • 我希望数组列表中包含一副纸牌中的所有纸牌。例如黑桃 A

标签: java for-loop arraylist


【解决方案1】:
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);
    }
}

我只复制了您的代码,此代码运行良好。我刚刚添加了 main 方法并调用了您的方法。而且,我得到的输出是

[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]

我相信这就是你想要的。

【讨论】:

  • 奇怪。也许我的环境有问题。还是谢谢你!
  • 嗯,是的,异常直接指向你的控制台类型。将卡片组中的每张卡片打印在单独的行上,错误可能会消失。
  • "异常:System.ArgumentOutOfRangeException:该值必须大于或等于零并且小于该维度中控制台的缓冲区大小。"是的,这是一个奇怪的错误,但它确实很清楚地表明出了什么问题,对吧?我不是要居高临下;我只是指出错误消息的内容相当重要。我的意思是遍历集合并打印出每个值,而不是一次打印出整个卡片组。
【解决方案2】:

问题现已解决。这是任何有同样问题的人的代码!

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】:

    这不是代码问题,而是环境默认配置值的结果。在 Windows Server 下,您可以更改控制台的默认屏幕缓冲区大小来避免它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 2011-09-29
      • 2016-02-23
      相关资源
      最近更新 更多