【问题标题】:Java arraylist all items are the sameJava arraylist所有项目都相同
【发布时间】:2017-04-03 03:18:27
【问题描述】:

为什么列表中的所有卡片都一样? 我试过hand.add(i,card); 但输出还是一样的。

Main.java

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
        List<Card> hand = new ArrayList();
        Card card = new Card((short) 7,"red");
        int b = 0;
        Random rn = new Random();
        for (int i=0;i<7;i++){
            card.setValue((short) (rn.nextInt((14 - 7) + 1) + 7));
            b = rn.nextInt(4);
            String[] colors = {"green","red","gold","brown"};
            card.setColor(colors[b]);
            System.out.println("Adding card to hand: " + card.getColor() + card.getValue() + " to: " +i);
            hand.add(card);
        }
        System.out.println("Your cards: ");
        for (Card k: hand) {
            System.out.println(k.show());
        }


    }
}

Card.java

public class Card {
    public short getValue() {
        return value;
    }

    public void setValue(short value) {
        this.value = value;
    }

    short value;

    public String getColor() {
        return color;
    }

    public void setColor(String farba) {
        this.color = farba;
    }

    String color;

    public Card(short value, String color) {
        this.value = value;
        this.color = color;
    }

    public String show(){
        return color + value;
    }
}

输出:

/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java
Adding card to hand: red13 to: 0
Adding card to hand: green10 to: 1
Adding card to hand: gold8 to: 2
Adding card to hand: brown10 to: 3
Adding card to hand: gold10 to: 4
Adding card to hand: gold8 to: 5
Adding card to hand: gold7 to: 6
Your cards: 
gold7
gold7
gold7
gold7
gold7
gold7
gold7

Lorem ipsum dolor sit amet,consectetur adipiscing elit。 Aenean nisl.Curabitur ac arcu ornare, aliquet eros eu, pretium massa。

【问题讨论】:

    标签: java list arraylist


    【解决方案1】:

    您在list 中为所有项目使用相同的 Card 对象,但您需要不同的对象(每个 Card 对象都有自己的颜色和值),所以你必须在for循环内创建new Card,如下所示:

        int b = 0;
        Random rn = new Random();
        for (int i=0;i<7;i++){
    
            b = rn.nextInt(4);
            String[] colors = {"green","red","gold","brown"};
    
            Card card = new Card((short) (rn.nextInt((14 - 7) + 1) + 7),colors[b]);
    
            System.out.println("Adding card to hand: " + 
                 card.getColor() + card.getValue() + " to: " +i);
    
            hand.add(card);
        }
    

    【讨论】:

      【解决方案2】:

      因为您使用的是在循环之前创建的一个卡片对象。将其更改为:

      public static void main(String[] args) {
              List<Card> hand = new ArrayList();
      
              int b = 0;
              Random rn = new Random();
              for (int i=0;i<7;i++){
      
                  Card card = new Card((short) 7,"red"); //changed
      
                  card.setValue((short) (rn.nextInt((14 - 7) + 1) + 7));
                  b = rn.nextInt(4);
                  String[] colors = {"green","red","gold","brown"};
                  card.setColor(colors[b]);
                  System.out.println("Adding card to hand: " + card.getColor() + card.getValue() + " to: " +i);
                  hand.add(card);
              }
              System.out.println("Your cards: ");
              for (Card k: hand) {
                  System.out.println(k.show());
              }
      
      
          } 
      

      这样每次迭代后都会创建一个新的卡片对象

      【讨论】:

        【解决方案3】:

        正如之前回答的那样,您的问题发生是因为您一直在将同一张卡添加到列表中。您所做的是每次更改对象的状态并将对同一对象的引用添加到列表中,因此每次更改时,对同一对象的所有引用都将显示相同的新值。

        在这种情况下,您需要做的是确保每次都创建一张新卡,并且在 for 循环中对新卡的引用永远不会改变,以避免意外覆盖。

        import java.util.ArrayList;
        import java.util.List;
        import java.util.Random;
        
        public class HelloWorld {
        
            public static void main(String[] args) {
                List<Card> hand = new ArrayList();
                int b = 0;
                Random rn = new Random();
                for (int i=0;i<7;i++){
                    final Card card = new Card((short) 7,"red");
                    card.setValue((short) (rn.nextInt((14 - 7) + 1) + 7));
                    b = rn.nextInt(4);
                    String[] colors = {"green","red","gold","brown"};
                    card.setColor(colors[b]);
                    System.out.println("Adding card to hand: " + card.getColor() + card.getValue() + " to: " +i);
                    hand.add(card);
                }
                System.out.println("Your cards: ");
                for (Card k: hand) {
                    System.out.println(k.show());
                }
            }
        }
        

        【讨论】: