Java-比较2个int数组并根据它们的值进行判断

Java-比较2个int数组并根据它们的值进行判断,java,arrays,compare,Java,Arrays,Compare,我有一组2个整数数组,包含相同数量的数字 比如说 int[] array1 = {1,3,5,5,2} int[] array2 = {5,3,4,4,4} 我需要根据两个标准对它们进行比较 有多少元素在同一索引上具有相同的值 有多少元素在不同索引上具有相同的值 并返回表示值的整数数组 我举了几个例子,以便您更好地理解: int[] array0 = {1,3,5,5,2}; int[] array1 = {5,3,4,4,4}; int[] array2 = {5,3,4,

我有一组2个整数数组,包含相同数量的数字

比如说

int[] array1 = {1,3,5,5,2}
int[] array2 = {5,3,4,4,4}
我需要根据两个标准对它们进行比较

  • 有多少元素在同一索引上具有相同的值
  • 有多少元素在不同索引上具有相同的值
  • 并返回表示值的整数数组

    我举了几个例子,以便您更好地理解:

       int[] array0 = {1,3,5,5,2};
       int[] array1 = {5,3,4,4,4};
       int[] array2 = {5,3,4,4,5};
       int[] array3 = {2,3,2,2,4};
       int[] array4 = {5,5,2,1,3};
       int[] array5 = {3,1,5,5,2};
       int[] array6 = {1,3,5,5,2};
    
       compare(array0,array1); //this would return 1,1
       compare(array0,array2); //this would return 1,2
       compare(array0,array3); //this would return 1,1
       compare(array0,array4); //this would return 0,5
       compare(array0,array5); //this would return 3,2
       compare(array0,array6); //this would return 5,0
    
    对于第一个数字很简单,我只需要检查array1的索引I上的元素是否与array2中的元素相同

    我在生成第二个数字时遇到问题,因为应该取其中一个数组中的最低数字。 如果我只看array1的元素是否在array2的某个地方,它在某些情况下会产生错误的结果

    如果你看

    int[] array1 = {1,3,5,5,2}
    

    检查数组1是否与索引上的数组3具有相同的内容,它将返回3个相等但位于不同位置的数字,这是错误的,因为它应该从最低的数字判断,结果应该是1

    如果我将其切换到比较array3在某些索引上是否与array1具有相同的内容,则它适用于这种情况,但不适用于其他情况

    关于如何处理这个问题,有什么想法吗?我很无知。

    • 克隆两个输入整数数组

    • 检查并查看元素在同一索引上是否具有相同的值

    • 如果是,请将1添加到同一索引计数器,并将输入整数数组中的值更改为-1(或小于最小有效值的数字)

    • 使用嵌套for循环检查元素是否具有相同的值。换句话说,检查第一个整数数组的第一个元素和第二个整数数组的所有元素。跳过两个数组中索引相同的元素,并跳过小于最小有效值的元素

    • 如果是,请将1添加到不同的索引计数器,并将输入整数数组中的值更改为-1(或小于最小有效值的数字)


    对于第二种情况,您可以尝试从数组中删除数字,并在找到匹配的数字后中断循环。可以将临时数组设置为原始数组的值。然后对照
    array3
    中的所有值检查
    array1[0]
    的值。找到匹配项后,从两个数组中删除该数字,并对照
    array3
    中的所有值检查
    array1[1]

    因此,当您将
    array1[4]
    array3[0]
    选中时,您将得到一个匹配项。然后,您将删除这两个数字并中断循环。没有更多的数字要签入
    数组1
    ,因此结果将是1


    我认为这将解决两次计算同一数值的问题。

    您可以有一组已经访问过的数字。您可能会这样:(不是测试此代码是否运行,只是给您一个想法)

    public int[]比较(int[]第一,int[]第二){
    Set numbersFoundInFirstArray=新的LinkedHashSet();
    Set numbersFoundInsectionArray=新的LinkedHashSet();
    int-insameinindex=0;
    int无差别指数=0;
    for(int i;i

    如果我记得清楚的话,集合中包含的比较有O(1)。Set的属性是您将只有一个特定类型的元素。因此,如果您将1添加两次,它将只有一个引用1。这样,您将只测试在另一个数组中是否已找到当前数字:)

    您需要将所有数组转换为树状结构,树状结构基本上将索引所有数组中的所有唯一值,并存储指向它们在数组中的位置和哪个数组的指针。该树的基本蓝图结构为:

    uniqElemVal->arrayIndx->arrayID = true
    
    如果uniqElemVal为1,3,5,2代表1,3,5,2,2,2,3,4代表2,3,2,2,4等,则所有人都加入工会

    arrayIndx将用于第一个数组0中的1,而5将用于2和3等

    数组ID是任意的,可以为每个数组设置关键帧,例如,第一个为1,第二个为2,等等

    因此,在这种情况下:

    #1st array
    1->0->1
    3->1->1
    5->2->1
    5->3->1
    2->4->1
    
    #2nd array
    2->0->2    
    3->1->2 
    3->2->2
    2->3->2
    2->4->2
    4->5->2
    
    然后,当您遍历此树时,每当您有一个具有多个叶的第二级节点时,这意味着在多个数组的特定位置上存在该特定元素值的匹配

    我个人将这棵树定义为

    HashMap<Integer, HashMap<Integer, ArrayList<Integer>>>
    
    HashMap
    

    因此,每当你有一个大于1个元素的叶数组列表时,这意味着你首先有一个匹配项,这听起来是个非常糟糕的主意。不要返回一个数组,其中每个索引都表示不同和神秘的内容。创建两种方法:compareSameIndex(数组,数组)和CompareDifferentinIndex(数组,数组)

    对于如何实现这些,您可以检查第一个数组中的每个索引,查看它们是否出现在第二个数组中的任何位置,并调用justcompare()。然后compareDifferentIndex()变为compare()-compareSameIndex()。例如:

    public int compare (int[] array0, int[] array1) {
      int matches = 0;
      List<Integer> list1 = Arrays.asList(array1);
    
      for (int curInt : array0) {
        if (list1.contains(curInt)) {
          matches++;
        }
      }
      return matches;
    }
    
    public int compareSameIndex(int[] array0, int[] array1) {
      int matches = 0;
      for (int i=0; i < array0.length; i++) {
        if (array0[i] == array1[i]) {
          matches++
        }
      }
      return matches;
    }
    
    public int compareDifferentIndex(int[] array0, int[] array1) {
      return compare(array0, array1) - compareSameIndex(array0, array1);
    }
    
    public int-compare(int[]array0,int[]array1){
    int匹配=0;
    List list1=Arrays.asList(array1);
    for(int curInt:array0){
    if(列表1.contains(curInt)){
    匹配++;
    }
    }
    返回比赛;
    }
    公共整数比较索引(整数[]数组0,整数[]数组1){
    int匹配=0;
    for(int i=0;iHashMap<Integer, HashMap<Integer, ArrayList<Integer>>>
    
    public int compare (int[] array0, int[] array1) {
      int matches = 0;
      List<Integer> list1 = Arrays.asList(array1);
    
      for (int curInt : array0) {
        if (list1.contains(curInt)) {
          matches++;
        }
      }
      return matches;
    }
    
    public int compareSameIndex(int[] array0, int[] array1) {
      int matches = 0;
      for (int i=0; i < array0.length; i++) {
        if (array0[i] == array1[i]) {
          matches++
        }
      }
      return matches;
    }
    
    public int compareDifferentIndex(int[] array0, int[] array1) {
      return compare(array0, array1) - compareSameIndex(array0, array1);
    }
    
     private int[] compare(int[] arr1, int[] arr2){
        int same_index = 0;
        Hashtable differences = new Hashtable();
        int diff_count = 0;
        if (arr1.length != arr2.length){
            throw new IllegalArgumentException("Array Size is not identical.");
        } else {
           for(int count = 0; count < arr1.length; count++){
              if (arr1[count] == arr2[count]{
                  same_index++;
                  differences.put(count, null);
              } else {
                  for (int count2 = 0; count2 < arr1.length; count2++){
                      if(!differences.containsKey(count2) && arr1[count] == arr2[count2]){
                          differences.put(count2, null);
                          diff_count++;
                      }
    
                  }                  
              }
           }
        }
        int[] returnArray = new int[2];
        returnArray[0] = same_count;
        returnArray[1] = diff_count;
        return (returnArray);
     }
    
    public void compare(int[] arrayA, int[] arrayB) {
        int sameIndex = 0;
        int diffIndex = 0;
        //Made two new empty arrays to save the status of each element in the corresponding array, whether it has been checked our not, if not, it'd be null.
        String[] arrayAstatus = new String[arrayA.length];
        String[] arrayBstatus = new String[arrayB.length];
        for (int i = 0; i < arrayA.length; i++) {            
            if (arrayA[i] == arrayB[i] || arrayAstatus[i] != null) {
                sameIndex++;
                continue;
            }
            for (int a = 0; a < arrayB.length; a++) {
                if (a == i || arrayBstatus[a] != null) {
                    continue;
                }
                if (arrayA[i] == arrayB[a]) {
                    arrayAstatus[i] = "checked";
                    arrayBstatus[a] = "checked";
                    diffIndex++;
                    break;
                }
            }
        }
        System.out.println(sameIndex + ", " + diffIndex);
    }