Java arraylist所有项都是一样的

时间:2016-11-19 09:21:12

标签: java list arraylist

为什么列表中的所有卡片都一样? 我试过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坐下来,精神上的精神。 Aenean nisl.Curabitur ac arcu ornare,aliquet eros eu,pretium massa。

3 个答案:

答案 0 :(得分:2)

您在list中使用相同的卡片对象,但您需要不同的对象(每个卡片对象都拥有它自己的颜色和值),因此您必须在new Card循环中创建for,如下所示:

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

答案 1 :(得分:1)

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

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


    } 

以便在每次迭代后创建一个新的卡对象

答案 2 :(得分:1)

之前已经回答过,您的问题发生了,因为您一直在向列表添加相同的卡片。你所做的是改变对象的状态并每次将对同一对象的引用添加到列表中,因此每次更改时,对同一对象的所有引用都将显示相同的新值。

在这种情况下,您需要做的是确保每次都创建一张新卡,并且在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());
        }
    }
}