Java:使用for循环填充ArrayList

huangapple go评论85阅读模式
英文:

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&#39;s buffer size in that dimension.

My Deck Class:

public class Deck{
    private final int deckSize = 52;
    private final String[] suit = {&quot;hearts&quot;, &quot;clubs&quot;, &quot;diamonds&quot;, &quot;spades&quot;};
    private final String[] face = {&quot;Ace&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;10&quot;,&quot;Jack&quot;,&quot;Queen&quot;,&quot;King&quot;};


    ArrayList&lt;String&gt; currentDeck = new ArrayList&lt;String&gt;(deckSize);


    public void getDeck(){
        for(int i=0; i&lt;face.length; i++) {
            for(int j=0; j&lt;suit.length; j++){
                String cards = face[i] + &quot; of &quot; + 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 = {&quot;hearts&quot;, &quot;clubs&quot;, &quot;diamonds&quot;, &quot;spades&quot;};
    private final String[] face = {&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot;};


    ArrayList&lt;String&gt; currentDeck = new ArrayList&lt;String&gt;(deckSize);


    public void getDeck() {
        for (int i = 0; i &lt; face.length; i++) {
            for (int j = 0; j &lt; suit.length; j++) {
                String cards = face[i] + &quot; of &quot; + 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 = {&quot;hearts&quot;,&quot;diamonds&quot;, &quot;clubs&quot;,  &quot;spades&quot;};
    private final String[] face = {&quot;Ace&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot;};

    ArrayList&lt;String&gt; currentDeck = new ArrayList&lt;String&gt;(deckSize);


    public void getDeck() {
        for (int i = 0; i &lt; face.length; i++) {
            for (int j = 0; j &lt; suit.length; j++) {
                if(j == 3){
                    String card = face[i] + &quot; of &quot; + suit[j];
                    currentDeck.add(card + &quot;\n&quot;);
                }
                else{
                String cards = face[i] + &quot; of &quot; + 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.

huangapple
  • 本文由 发表于 2020年4月7日 00:38:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定