Java数组对象错误(卡)

时间:2017-05-31 18:09:15

标签: java arrays oop null

让我们开始,3个班级(卡片,开始,Deckofcards)

1º卡:

public class Card {
    private String type;
    private int value;
    private String number;



    public Card(String number,String type,int value) {
        this.type=type;
        this.value=value;
        this.number=number;
    }
    public String gettype() {
        return type;
    }
    public String getNumber() {
        return number;
    }
    public int getValue() {
        return value;
    }
    @Override
    public String toString() {
        return getNumber() + " de " + gettype() + "  --> VALUE " + getValue() ;
        }
    }

2º开始

public class Start {

   public static void main(String[] args) {
        Deckofcards deckofcards = new Deckofcards();
        Card c = deckofcards.newCard();
        String s =c.toString();
        System.out.println(s);

        Card c2 = deckofcards.newCard();
        String s2 =c2.toString();
        System.out.println(s2);
    }
   }

然后,问题在这里

Deckofcards:

public class Deckofcards {
        private Card CardsR[];
        private final Card[] Cards = {
            new Card("A","heart",1),
            new Card("2","heart",2),
            new Card("3","heart",3),
            new Card("4","heart",4),
            new Card("5","heart",5),
            new Card("6","heart",6),
            new Card("7","heart",7),
            new Card("8","heart",8),
            new Card("9","heart",9),
            new Card("10","heart",10),
            new Card("J","heart",10),
            new Card("Q","heart",10),
            new Card("K","heart",10),
            new Card("As","Diamond",1),
            new Card("2","Diamond",2),
            new Card("3","Diamond",3),
            new Card("4","Diamond",4),
            new Card("5","Diamond",5),
            new Card("6","Diamond",6),
            new Card("7","Diamond",7),
            new Card("8","Diamond",8),
            new Card("9","Diamond",9),
            new Card("10","Diamond",10),
            new Card("J","Diamond",10),
            new Card("Q","Diamond",10),
            new Card("K","Diamond",10),
            new Card("A","clover",1),
            new Card("2","clover",2),
            new Card("3","clover",3),
            new Card("4","clover",4),
            new Card("5","clover",5),
            new Card("6","clover",6),
            new Card("7","clover",7),
            new Card("8","clover",8),
            new Card("9","clover",9),
            new Card("10","clover",10),
            new Card("J","clover",10),
            new Card("Q","clover",10),
            new Card("K","clover",10),
            new Card("A","Spades",1),
            new Card("2","Spades",2),
            new Card("3","Spades",3),
            new Card("4","Spades",4),
            new Card("5","Spades",5),
            new Card("6","Spades",6),
            new Card("7","Spades",7),
            new Card("8","Spades",8),
            new Card("9","Spades",9),
            new Card("10","Spades",10),
            new Card("J","Spades",10),
            new Card("Q","Spades",10),
            new Card("K","Spades",10),
        };

    //Sorry if the translation its not correct, this is only a little part of a 
    //big code.
        public Deckofcards() {
            Collections.shuffle(Arrays.asList(Cards)); //SHUFFLE
            CardsR=Cards.clone(); //COPY
        }
        public Card newCard(){
            boolean while1 = true;
            Card take=null;
                for(int i = 0; i < CardsR.length; i++){
                    while(while1){
                    if(CardsR[i]!=null){
                        take=Cards[i];
                        //CardsR[i]=null;
                        while1=false;

                    }
                }
            }
            return take;
        }
    }

我们来解释一下。

我有一系列卡片,我混合。 好。所有正确的

然后我打电话给一张卡片(课程:开始)。 好。所有正确的

---然后--- 当我再次打电话时,该卡返回相同的值... 我试图设置一个null ...然后它无缘无故地启动一个无限循环?。

任何可能的解决方案? 感谢

3 个答案:

答案 0 :(得分:1)

你并没有真正对洗牌的名单做任何事情;该实例本质上是瞬态的,因此是垃圾收集的。请注意,将您创建的列表从阵列中移除,不会随机播放阵列。相反,你可以这样做:

List<Card> cardList = Arrays.asList(Cards);
Collections.shuffle(cardList);

CardsR = cardList.toArray(new Card[cardList.size()]);

您的newCard方法不需要这么复杂。我假设你只想从洗牌阵列中返回一张牌。一种方法是保持最后一张卡的索引;您可以在构造函数中将其初始化为-1。在newCard中,只要您没有超出范围,您将递增此索引并在该索引处返回该卡。如果是,您可能需要打印一条消息,说明没有卡片(例如)。

其他一些指示:

  • 确保遵循Java命名约定;字段和变量名称不应大写。
  • 考虑使用List<Card>卡而不是数组;这样你就可以简单地进行洗牌,而不是在数组和列表之间来回转换。

答案 1 :(得分:1)

  

当我再次打电话时,该卡返回相同的值

因为

            for(int i = 0; i < CardsR.length; i++){  // i = 0 
                while(while1){                       // true
                if(CardsR[i]!=null){                 // true
                    take=Cards[i];                   //
                    while1=false;                    // so no more while loop execution 
                                                     // first card will always returned
  

我试图设置一个null ...然后它会启动无限循环   原因是什么?

在第一次通话Card c = deckofcards.newCard();期间CardsR[0]设置为空,因此在第二次通话期间Card c2 = deckofcards.newCard();您的if条件if(CardsR[i]!=null)将永远不会被执行,现在您陷入无限while循环

  boolean while1 = true;
            Card take=null;
                for(int i = 0; i < CardsR.length; i++){
                    while(while1){
                    if(CardsR[i]!=null){
                        take=Cards[i];
                        //CardsR[i]=null;
                        while1=false;

                    }
                }
            }

解决方案:您只需使用Random实例从数组中选择并返回随机cards

How to randomly pick an element from an array

答案 2 :(得分:0)

take的值将始终是数组中的第一张卡片。

当您将值设置为null时,while循环永远不会终止,因为while2仅在找到非空条目时设置为false。找到null后,没有任何反应,while循环会永远运行,因为while2无法设置为false

break语句应该在这里诀窍

public Card newCard(){
        Card take=null;
            for(int i = 0; i < CardsR.length; i++){
                if(CardsR[i]!=null){
                    take=Cards[i];
                    CardsR[i]=null;
                    break;
                }
            }
        }
        return take;
    }

break语句将退出它所在的循环。

或者,您可以使用List<Card>而不是数组,使用该数组可以从随机列表中删除条目并将其返回,从而无需设置和检查null

private List<Card> cardList;

public Deckofcards() {
        cardList = Collections.shuffle(Arrays.asList(Cards)); //SHUFFLE
        }

public Card newCard(){
    if(!cardList.isEmpty()){
       return cardList.remove(0);        
    }            
    return null;
}