简体   繁体   English

如何在Java中将数组元素与另一个数组进行比较

[英]how to compare elements of array with another array in java

logic is of setgame 逻辑是设定游戏

boolean isset = false;

three card array each with 4 different propert 三个卡阵列,每个阵列具有4个不同的属性

String[] arraycard1 = {"1","open","red","diamond"};
String[] arraycard2 ={"1","solid","green","diamond"};
String[] arraycard3 ={"1","open","purple","oval"};

a set is any combination of three cards in which each property is the same on all three cards, or different on all three cards 一组是三张牌的任意组合,其中,每张三张牌的每个属性都相同,或者所有三张牌的属性不同

I need to check If this set of 3 cards is set or not. 我需要检查是否设置了这套3张卡。

for (int i=0 ; i<arraycard1.length ; i++){

checking if properties are all same or all different 检查属性是否全部相同或全部不同

      if ((arraycard1[i].equalsIgnoreCase(arraycard2[i]) && arraycard1[i].equalsIgnoreCase(arraycard3[i]) && arraycard2[i].equalsIgnoreCase(arraycard3[i]))||(arraycard1[i]!=arraycard2[i] && arraycard1[i]!=arraycard3[i] && arraycard2[i]!=arraycard3[i])){
              isset = true;
      }
}

Why not use a class and override the equals method? 为什么不使用类并覆盖equals方法?

import java.awt.Color;

public class Card{
    int number;
    String state;
    Color color;
    String suit;

    public Card(int number, String state, Color color, String suit) {
        super();
        this.number = number;
        this.state = state;
        this.color = color;
        this.suit = suit;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((color == null) ? 0 : color.hashCode());
        result = prime * result + number;
        result = prime * result + ((state == null) ? 0 : state.hashCode());
        result = prime * result + ((suit == null) ? 0 : suit.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Card other = (Card) obj;
        if (color == null) {
            if (other.color != null)
                return false;
        } else if (!color.equals(other.color))
            return false;
        if (number != other.number)
            return false;
        if (state == null) {
            if (other.state != null)
                return false;
        } else if (!state.equals(other.state))
            return false;
        if (suit == null) {
            if (other.suit != null)
                return false;
        } else if (!suit.equals(other.suit))
            return false;
        return true;
    }
}

If you are using Eclipse IDE all of this is generated automatically, you only have to declare the 4 fields. 如果使用的是Eclipse IDE,则所有这些都是自动生成的,只需声明4个字段即可。

From the menù: 从菜单:

  • Source -> Generate getters and setters. 源->生成获取器和设置器。
  • Source -> Generate constructor using fields. 源->使用字段生成构造函数。
  • Source -> Generate hashCode() and equals(). 源->生成hashCode()和equals()。

Then you can create a Card object: 然后,您可以创建Card对象:

Card card1 = new Card(1, "open", Color.RED, "diamond");
Card card2 = new Card(1, "solid", Color.GREEN, "diamond");

boolean sameCard = card1.equals(card2); //false

You can also create a CardUtils class which can check some basic combinations: 您还可以创建CardUtils类,该类可以检查一些基本组合:

public class CardUtils{

    public static boolean isPair(Card a, Card b){
        return a.getNumber() == b.getNumber() && //Same number, different suit
               !a.getSuit().equals(b.getSuit());
    }

    public static boolean isFlush(Card.. cards){
        String suit = carsds[0].getSuit();
        for(Card c: cards){
             if(!c.getSuit().equals(suit))
                 return false;
        return true;
    }
    public static boolean isPoker(Card..cards){
         if(cards.lenght!=5) return false;
         for(int i = 0; i < 2; i++){
             int count = 0;
             for(Card c1: cards)
                 if(isPair(cards[i], c1)) count++;
             if(count==3) return true; //There are other 3 cards with same number but different suit in hand -> Poker!
         }
         return false;
    }
}

And use it 并使用它

Card card1 = new Card(1, "open", Color.RED, "diamond");
Card card2 = new Card(1, "solid", Color.GREEN, "spears");
Card card3 = new Card(2, "solid", Color.RED, "diamond");

CardUtils.isPair(card1, card2); //True
CardUtils.isPair(card1, card3); //False

CardUtils.isFlush(card1, card2, card3); //False
CardUtils.isFlush(card1, card3); //True

Last but not least you can use Enums to deal with the suits: 最后但并非最不重要的一点是,您可以使用Enums处理西装:

public enum Suit{
    DIAMONDS,
    HEARTS,
    SPADES,
    CLUBS
}
Card card1 = new Card(1, "open", Color.RED, Suit.DIAMONDS);

You can use ArrayaList to for quicker comparison.Assign the string array to an Array list and use the .equals () ? 您可以使用ArrayaList进行更快的比较。将字符串数组分配给Array列表并使用.equals()? Method 方法

Here is an example: 这是一个例子:

String[] array1 = {"1", "2", "3"};

String[] array2 = {"1", "2", "3"};

String[] array3 = {"1", "2", "3"};

List<List<String>> lst1 = new      ArrayList<>();
lst1.add(Arrays.asList(array1));

List<List<String>> lst2 = new ArrayList<>();
lst2.add(Arrays.asList(array2));

List<List<String>> lst3 = new ArrayList<>();
lst3.add(Arrays.asList(array3));
System.out.println(lst1.equals(lst2) && lst1.equals(lst3));     //prints true

You could try something like this. 您可以尝试这样的事情。

public static void main(String[] arraycard1,String[] arraycard2,String[] arraycard3) {
  String[] arraycard1 = {"1","open","red","diamond"};
  String[] arraycard2 ={"1","solid","green","diamond"};
  String[] arraycard3 ={"1","open","purple","oval"};

  System.out.println("Is array 1 equal to array 2?? "
  +Arrays.equals(arraycard1, arraycard2));

  System.out.println("Is array 1 equal to array 3?? "
  +Arrays.equals(arraycard1, arraycard3));

  System.out.println("Is array 2 equal to array 3?? "
  +Arrays.equals(arraycard2, arraycard3));
}

RESULT 结果

Is array 1 equal to array 2?? false
Is array 1 equal to array 3?? false
Is array 2 equal to array 3?? false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM