【问题标题】:How to stop null from printing如何阻止null打印
【发布时间】:2019-06-14 02:45:16
【问题描述】:

执行我的程序时,它在第 13 行一直显示“null”我想知道我的算法出了什么问题,因为它一直打印 null。

private class SpadeIterator implements Iterator<Card>{
    private int nextCardSpade;
    private List<Card> cards;
    private int count=0;
    private SpadeIterator(List cards) {
        this.cards=cards;
        this.nextCardSpade = cards.size()-1;
    }

    @Override
    public boolean hasNext() {
        count++;
        if(nextCardSpade<0)
            return false;
        //nextCardSpade--;
        return true;
    }

   @Override
    public Card next() {

        int i=0;
        this.count=i;
        Card temp = cards.get(nextCardSpade);

        while(hasNext()){    //find SPADES
            temp=cards.get(nextCardSpade--);
            i++;

            if(temp.suit.value == Suit.SPADES.value)
                return temp;
        }
        //DONT MOVE
        return null;
        //nextCardSpade--;      //DONT DELETE

    }
}

Current Results

结果旨在显示 13 个黑桃,最后不返回 null。

【问题讨论】:

  • 然后只检查null
  • 您的next() 方法不应包含返回null 的任何大小写。如果没有合适的元素被返回,那么它就是hasNext() 方法作业返回false
  • 您的hasNext 方法也不应该以任何方式修改对象的状态(增加计数)。
  • 是的,但我需要返回一些东西,否则程序无法运行
  • 如果没有下一个,那么next 应该抛出 NoSuchElementException,而不是返回值。 docs.oracle.com/javase/7/docs/api/java/util/…。除非hasNext 说有下一个元素,否则大多数调用者都会避免调用next

标签: java null java-print


【解决方案1】:

检查 nextCardSpade 是否也等于 0:

if (nextCardSpade <= 0)

【讨论】:

    【解决方案2】:

    您的next() 方法不应包含任何会返回无效值的情况,例如null。如果没有要返回的下一个元素,则 hasNext() 方法的 job 返回 false 以防止您调用 next()

    所以你的代码应该看起来更像

    class SpadeIterator implements Iterator<Card>{
        private int spadesCounter = 0;
        private Iterator<Card> cardsIt;
    
        private SpadeIterator(List<Card> cards) {
            cardsIt = cards.iterator();
        }
    
        @Override
        public boolean hasNext() {
            return spadesCounter<13; // we can't put spacesCounter++ here because 
                                     // we should be able to call `hasNext()` many times
                                     // and still get same answer,
                                     // so `hasNext()` shouldn't change any state 
                                     // (at least one which could cause changing its result)
        }
    
        @Override
        public Card next() {
            Card temp = cardsIt.next(); //if our `hasNext()` returned `false` but we 
                                        //didn't check or ignored it, this will CORRECTLY 
                                        //throw NoSuchElementException 
            while(temp.suit.value != Suit.SPADES.value){
                temp = cardsIt.next();
            }
            spadesCounter++;
            return temp;
        }
    }
    

    或者,如果您只是想遍历列表并仅打印选定的元素,您可以使用带有过滤功能的流

    List<Card> cards = ...//not really important how get it
    cards.stream()
         .filter(card -> card.suit.value == Suit.SPADES.value)
         .forEach(card -> System.out.println(card));
    

    甚至更简单

    for (Card card : cards){
        if(card.suit.value == Suit.SPADES.value){
            System.out.println(card);
        }
    }
    

    【讨论】:

    • 当没有从next() 返回的下一个元素时,需要抛出 NoSuchElementException。在您的代码中,这隐含在对 cardsIt.next() 的调用中。您可能知道这一点,我只是为原始海报指出。
    • @another-dave 感谢您指出这一点。我稍微更新了我的答案以包含此信息。
    猜你喜欢
    • 2012-01-13
    • 2018-08-05
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    相关资源
    最近更新 更多