在不破坏编译的情况下不能反转数组

时间:2018-10-21 07:11:51

标签: java

目前,我正在尝试获取要在测试类中向后打印的列表。我以为Collections.reverse(discardPile)可以工作,所以我用了它,而且结果是混合的,至少可以这样说。

到目前为止,这是我的代码

           DiscardPile<Card> discardPile = null; 
       discardPile = new DiscardPile<Card>(52, 0);
           discardPile.push(new Card(8));
        discardPile.push(new Card(32));
        discardPile.push(new Card(48));
        discardPile.push(new Card(2));
        discardPile.push(new Card(17));
        discardPile.push(new Card(20)); //removeTopCard should remove all that's above
        discardPile.push(new Card(25));
        discardPile.push(new Card(50));
        discardPile.push(new Card(19));
        discardPile.push(new Card(41)); //10 Cards that must be popped
        //Collections.reverse(discardPile); //texted it out for the meantime
        System.out.println(discardPile.toString()); 

        Card[] cardArr = discardPile.removeTopCard(new Card(50));
        for(Card co : cardArr) { //for loop to print the method removeTopCards
            System.out.println(co);
            }                
        }

目标是要打印出dropPile.push()系列

9 of Diamonds
A of Diamonds
K of Spades
8 of Diamonds
4 of Spades

并丢弃Pile.removeTopCard(新卡(50))进行打印

    K of Spades
    8 of Diamonds
    4 of Spades

没有Collections.reverse(discardPile) 我的代码打印

[10 of Clubs
, 8 of Hearts
, J of Spades
, 4 of Clubs
, 6 of Diamonds
, 9 of Diamonds
, A of Diamonds
, K of Spades
, 8 of Diamonds
, 4 of Spades
]
4 of Spades

8 of Diamonds

K of Spades

用它以正确的顺序打印所有带括号的数字,但是下一连串的字符串已经不正确了。它可以打印3张以上的卡片。

这是我的卡类

public class Card {

    private int rank;

    private int suit;


    public Card() {
        rank = (int) (Math.random() * 13);
        suit = (int) (Math.random() * 4);
    }


    public Card(int n) {
        if (n >= 0 && n <= 51) {
            rank = n % 13;
            suit = n / 13;
        }
    }


    public Card(int r, int s) {
        if ((r >= 0 && r <= 12) && (s >= 0 && s <= 3)) {
            rank = r;
            suit = s;
        }
    }


    public String toString() {
        String c = getRankAsString() + " " + "of " + getSuitAsString() + "\n";
        c = c.replace(",", "").replace("[", "").replace("]", "");
        return c;
        }


    public void setRank(int r) {
        if (r >= 0 && r <= 12)
            rank = r;
    }


    public void setSuit(int s) {
        if (s >= 0 && s <= 3)
            suit = s;
    }


    public int getRank() {

        return rank;
    }


    public int getSuit() {
        return suit;
    }


    public String getRankAsString() {
        String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; // 0-12
        return ranks[rank];
    }


    public String getSuitAsString() {
        String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; // 0-3
        return suits[suit];
    }


    public boolean equals(Card otherCard) {
        return ((rank == otherCard.rank) && (suit == otherCard.suit));
    }


    public int compareByRank(Card otherCard) {
        return rank - otherCard.rank;
    }


    public int compareBySuit(Card otherCard) {
        return suit - otherCard.suit;
    }
}

这是我的丢弃堆类

import generics.StackEmptyException;
import generics.StackFullException;

import java.util.Stack;

public class DiscardPile<T> extends Stack<T>  { //subclass of its parent Stack

         public DiscardPile(int i, int z) {
         super();
         }

    public Card[] removeTopCard(Card otherCard) throws StackEmptyException {

        Card[] theCard = new Card[size()];
        int i = 0;
        boolean check = false;      
        if(isEmpty()){ //in the case that there's nothing there
          throw new StackEmptyException("Stack is Empty");
        }
        while(!isEmpty()){ //If it's not empty, pop the top card (unlimited loop that'll need to be broken by a break;)         
          Card top = (Card) pop(); //Casts Card to pop, which should remove it
          theCard[i] = top; //removed and returned in an array
          i++; 
          if(top.equals(otherCard)){ //checks if the card's rank and suit are the same and stops the loop if true
            check = true;
            break; //it ends the loop if they're equivalent, all of those cards were set to top
          }
        }
        if(!check){ //if it can't find anything
          throw new StackEmptyException("Card not found");
        }
        Card[] topCards = new Card[i];
        for(int j = 0;j < i; j++){ //for loop through the popped cards

          topCards[j] = theCard[j]; //set the discarded value array to theCard
        }
        return topCards; //should return the popped numbers

      }

1 个答案:

答案 0 :(得分:0)

据我了解,您想使用Stack数据结构。

Stack stack = new Stack();
stack.push(discardPile.push(new Card(32))); .......

然后,您可以使用stack.peek()到达栈顶,并且可以有效地完成所有其他与栈相关的操作。如果您不了解堆栈数据结构,请遵循此文档https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm