Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

java - How would I check if all elements in an array are present in another array with no duplicates?

Run into abit of trouple with my code, want to check if sharedMemory is present in entire deck(it is) and have no duplicates of an object. I am getting false with this code and I don't really know why, any help would be appreciated. rank and suit together form a card object. rank and card are enumerators with values. Note: when i use 'return Arrays.asList(entireDeck).containsAll(sharedMemory)' on its own it does show true, but the messy if part is trying to check for duplicates

 public static boolean isFull(){
        Card[] entireDeck = Deck.fillDeck();
        sharedMemory =Arrays.asList(Deck.fillDeck());
        int i=0,duplicates=0, position =0, original;
        for(Card c:entireDeck){
            Card f = new Card(c.rank, c.suit);
            original=i;
            if (f.rank.equals(sharedMemory.get(i).rank)&&f.suit.equals(sharedMemory.get(i).suit)){
                duplicates+=1;
                position=i;
                for(i=0; i<position;i++) {
                    if (f.rank.equals(sharedMemory.get(i).rank)&&f.suit.equals(sharedMemory.get(i).suit)) 
               {
                        duplicates += 1;
                        return false;
                    }
                }
                for (i=position+1; i<52; i++){
                    if (f.rank.equals(sharedMemory.get(i).rank)&&f.suit.equals(sharedMemory.get(i).suit)) 
              {
                        duplicates += 1;
                        return false;
                    }
                }
                if(duplicates>1){
                    return false;
                }
                else{
                    i=original;
                }

            }
            i=original;
            i++;
        }
        return Arrays.asList(entireDeck).containsAll(sharedMemory);
    }

Thanks :)

question from:https://stackoverflow.com/questions/65946724/how-would-i-check-if-all-elements-in-an-array-are-present-in-another-array-with

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The easiest way is to use a Set<Card>. If the return value of adding an object to a set is false, then it means the set already contains it, thus a duplicate. For this to work the Card class must override both hashCode and equals.

Set<Card> set = new HashSet<>();
for(Card card : cardArray) {
    if (!set.add(card)) { // if false then !false is true so signal duplicate.
        System.out.println("Duplicate of " + card + " found);
        break;
    }
}

You can always sort the Cards and then do a one to one comparison to see if they are equal. Comparing adjacent cards of a sorted deck can also detect duplicates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...