Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中将数组的元素与另一个数组进行比较_Java_Arrays - Fatal编程技术网

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

如何在java中将数组的元素与另一个数组进行比较,java,arrays,Java,Arrays,逻辑是游戏的基础 boolean isset = false; 三卡阵列,每个阵列具有4种不同的属性 String[] arraycard1 = {"1","open","red","diamond"}; String[] arraycard2 ={"1","solid","green","diamond"}; String[] arraycard3 ={"1","open","purple","oval"}; 集合是三张牌的任意组合,其中三张牌上的每个属性相同,或三张牌上的每个属性不同 我

逻辑是游戏的基础

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++){

你可以试试这样的

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));
}
结果

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

你可以试试这样的

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));
}
结果

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

你可以试试这样的

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));
}
结果

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

你可以试试这样的

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));
}
结果

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

您可以使用ArrayList进行快速比较。将字符串数组指定给数组列表并使用.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
String[]array1={“1”、“2”、“3”};
字符串[]数组2={“1”、“2”、“3”};
字符串[]数组3={“1”、“2”、“3”};
List lst1=new ArrayList();
lst1.add(Arrays.asList(array1));
List lst2=新的ArrayList();
lst2.add(Arrays.asList(array2));
List lst3=新的ArrayList();
lst3.add(Arrays.asList(array3));
System.out.println(lst1.equals(lst2)和&lst1.equals(lst3))//打印正确

您可以使用ArrayList进行快速比较。将字符串数组指定给数组列表并使用.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
String[]array1={“1”、“2”、“3”};
字符串[]数组2={“1”、“2”、“3”};
字符串[]数组3={“1”、“2”、“3”};
List lst1=new ArrayList();
lst1.add(Arrays.asList(array1));
List lst2=新的ArrayList();
lst2.add(Arrays.asList(array2));
List lst3=新的ArrayList();
lst3.add(Arrays.asList(array3));
System.out.println(lst1.equals(lst2)和&lst1.equals(lst3))//打印正确

您可以使用ArrayList进行快速比较。将字符串数组指定给数组列表并使用.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
String[]array1={“1”、“2”、“3”};
字符串[]数组2={“1”、“2”、“3”};
字符串[]数组3={“1”、“2”、“3”};
List lst1=new ArrayList();
lst1.add(Arrays.asList(array1));
List lst2=新的ArrayList();
lst2.add(Arrays.asList(array2));
List lst3=新的ArrayList();
lst3.add(Arrays.asList(array3));
System.out.println(lst1.equals(lst2)和&lst1.equals(lst3))//打印正确

您可以使用ArrayList进行快速比较。将字符串数组指定给数组列表并使用.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
String[]array1={“1”、“2”、“3”};
字符串[]数组2={“1”、“2”、“3”};
字符串[]数组3={“1”、“2”、“3”};
List lst1=new ArrayList();
lst1.add(Arrays.asList(array1));
List lst2=新的ArrayList();
lst2.add(Arrays.asList(array2));
List lst3=新的ArrayList();
lst3.add(Arrays.asList(array3));
System.out.println(lst1.equals(lst2)和&lst1.equals(lst3))//打印正确

为什么不使用类并重写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;
    }
}
如果您使用的是EclipseIDE,那么所有这些都是自动生成的,您只需声明4个字段

从男人那里:

  • Source->生成getter和setter
  • 源->使用字段生成构造函数
  • Source->Generate hashCode()和equals()
然后,您可以创建卡片对象:

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;
    }
}
最后但并非最不重要的一点是,您可以使用枚举来处理诉讼:

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

为什么不使用类并重写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;
    }
}
如果您使用的是EclipseIDE,那么所有这些都是自动生成的,您只需声明4个字段

从男人那里:

  • Source->生成getter和setter
  • 源->使用字段生成构造函数
  • Source->Generate hashCode()和equals()
然后,您可以创建卡片对象:

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;
    }
}
最后但并非最不重要的一点是,您可以使用枚举来处理诉讼:

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

为什么不使用类并重写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;
    }
}
如果您使用的是EclipseIDE,那么所有这些都是自动生成的,您只需声明4个字段

从男人那里:

  • Source->生成getter和setter
  • 源->使用字段生成构造函数
  • Source->Generate hashCode()和equals()
然后,您可以创建卡片对象:

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;
    }
}
最后但并非最不重要的一点是,您可以使用枚举来处理诉讼:

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

为什么不使用类并重写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;
    }
}
如果您使用的是EclipseIDE,那么所有这些都是自动生成的,您只需声明4个字段

从男人那里:

  • Source->生成getter和setter
  • 源->使用字段生成构造函数
  • Source->Generate hashCode()和equals()
然后,您可以创建卡片对象:

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;
    }
}
最后但并非最不重要的一点是,您可以使用枚举来处理诉讼:

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

所以在16个元素中。如果一个元素是不同的,那么它“完全不同”?或者因为一个数组中的4个元素中有3个是相同的,所以它是“相同的”吗?请更详细地解释你想要实现的目标。因此,在16个要素中。如果一个元素是不同的,那么它“完全不同”?或者因为一个数组中的4个元素中有3个是相同的,所以它是“相同的”吗?请更详细地解释你想要实现的目标。因此,在16个要素中。如果一个元素是不同的,那么它“完全不同”?或者因为一个数组中的4个元素中有3个是相同的,所以它是“相同的”吗?请更详细地解释你想要实现的目标。因此,在16个要素中。如果一个元素是不同的,那么它“完全不同”?或者因为一个数组中的4个元素中有3个是相同的,所以它是“相同的”吗?他写了
equalsIgnoreCase
else if(!suit.equals(other.suit))
->
else if(!suit.equalsIgno