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

时间:2014-09-09 06:16:45

标签: java arrays

逻辑是setgame

boolean isset = false;

三个卡阵列,每个都有4个不同的属性

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

一套是三张牌的任意组合,其中每张属性在所有三张牌上都相同,或者在所有三张牌上都不同

我需要检查是否设置了这组3张牌。

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

检查属性是否全部相同或全部

      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;
      }
}

3 个答案:

答案 0 :(得分:2)

为什么不使用类并覆盖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;
    }
}

如果您使用的是Eclipse IDE,则所有这些都是自动生成的,您只需要声明4个字段。

来自menù:

  • 来源 - &gt;生成getter和setter。
  • 来源 - &gt;使用字段生成构造函数。
  • 来源 - &gt;生成hashCode()和equals()。

然后你可以创建一个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

您还可以创建一个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;
    }
}

并使用它

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

最后但并非最不重要的是,您可以使用Enums来处理诉讼:

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

答案 1 :(得分:1)

您可以使用ArrayaList来更快地进行比较。将字符串数组分配给数组列表并使用.equals()?方法

以下是一个例子:

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

答案 2 :(得分:0)

你可以尝试这样的事情。

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