用于在多个二维数组中搜索相似条目的代码 [英] Code for searching similar entires in multiple two-dimensional arrays

查看:94
本文介绍了用于在多个二维数组中搜索相似条目的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写中描述的问题的代码我以前的话题。建议的解决方案是使用hashmaps在多个数组中找到类似的条目(数组有相同的列数,但它们可能有不同的行数)

我的示例代码基于用户John B的代码片段提供了这里。为了简单和调试目的,我创建了3个不同的一维行而不是二维数组。另外,为简单起见,函数 equalRows 应该返回 true false 而不是行索引。

因此,在下面的代码中,函数 equalRows 应该返回 false ,因为 array3 {1,3,4} ,它确实有 {1,2,3} 。相反,函数返回 true 。为什么会发生?

  import java.util.HashMap; 
import java.util.Map;

public class Test {

public static void main(String [] args){
int [] array1 = {1,2,3};
int [] array2 = {1,2,3};
int [] array3 = {1,3,4};
boolean answ = equalRows(array1,array2,array3);
System.out.println(answ);
}

static class Row extends Object {
private int value;
private volatile int hashCode = 0;

public Row(int val){
this.value = val;

$ b @Override
public boolean equals(Object obj){
if(this == obj)
return true; $(bb)if((obj == null)||(obj.getClass()!= this.getClass()))
return false;
//对象必须是此时的行
行列=(Row)obj;
return(value == row.value);


@Override
public int hashCode(){
final int multiplier = 7;
if(hashCode == 0){
int code = 31;
code = multiplier * code + value;
hashCode = code;
}
返回hashCode;
}
}

private static Map< Row,Integer> map(int [] array){
Map< Row,Integer> arrayMap = new HashMap< Row,Integer>();
for(int i = 0; i< array.length; i ++)
arrayMap.put(new Row(array [i]),i);
返回arrayMap;

$ b $ private static boolean equalRows(int [] array1,int [] array2,int [] array3){
Map< Row,Integer> map1 = map(array1);
Map< Row,Integer> map2 = map(array2);

for(int i = 0; i< array3.length; i ++){
Row row = new Row(array3 [i]);
整数array1Row = map1.get(row);
整数array2Row = map2.get(row);
if(array1Row!= null || array2Row!= null){
return false;
}
}
返回true;
}

}

编辑#1
代码根据建议的解决方案更新。



编辑#2
我检查了建议的解决方案,但该函数即使对于:int [] array1 = {1,2,3};也会返回false。 int [] array2 = {1,2,3}; int [] array3 = {1,2,3},虽然它应该是真的。我认为问题出在函数 hashcode 上。那么,有什么解决方法?

解决方案

这行是错误的,它立即返回true:

  if(array1Row!= null&& array2Row!= null){
return true;

$ / code>

你必须做的是(完全反转逻辑):

  if(array1Row == null || array2Row == null){
return false;
}


I´m trying to write the code for the problem described in my previous topic. The suggested solution was to use hashmaps to find similar entries in multiple arrays (arrays have the same number of columns, but they might have different number of rows).

Below is my sample code based on a code snippet of the user John B provided here. For simplicity and for debugging purpose, I created just 3 different one-dimensional rows instead of two-dimensional arrays. Also, for simplicity, the function equalRows should return true or false instead of row indexes.

So, in the below code the function equalRows should return false, because array3 has {1,3,4} and it does have {1,2,3}. Instead the function returns true. Why does it happen?

import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) {
        int[] array1 = {1,2,3}; 
        int[] array2 = {1,2,3}; 
        int[] array3 = {1,3,4};
        boolean answ = equalRows(array1,array2,array3);
        System.out.println(answ);
    }

    static class Row extends Object {
        private int value;
        private volatile int hashCode = 0;

        public Row(int val) {
            this.value = val;
        }

        @Override
        public boolean equals(Object obj) {
            if(this == obj)
                return true;
            if((obj == null) || (obj.getClass() != this.getClass()))
                return false;
            // object must be Row at this point
            Row row = (Row)obj;
                return (value == row.value);
        }

        @Override
        public int hashCode () {
            final int multiplier = 7;
            if (hashCode == 0) {
                int code = 31;
                code = multiplier * code + value;
                hashCode = code;
            }
            return hashCode;
        }
    }

    private static Map<Row, Integer> map(int[] array) {
          Map<Row, Integer> arrayMap = new HashMap<Row, Integer>();
          for (int i=0; i<array.length; i++)
                arrayMap.put(new Row(array[i]), i);
          return arrayMap;
    }

    private static boolean equalRows(int[] array1, int[] array2, int[] array3){
           Map<Row, Integer> map1 = map(array1);
           Map<Row, Integer> map2 = map(array2);

           for (int i=0; i<array3.length; i++){
              Row row = new Row(array3[i]);
              Integer array1Row = map1.get(row);
              Integer array2Row = map2.get(row);
              if (array1Row != null || array2Row != null) {
                  return false;
              }
           }
        return true;
    }

}

Edit#1 Code is updated subject to suggested solution.

Edit#2 I checked out the suggested solution, but the function returns false even for: int[] array1 = {1,2,3}; int[] array2 = {1,2,3}; int[] array3 = {1,2,3}, although it should be true. I think the problem is with the function hashcode. So, any solution?

解决方案

This line is wrong, it immediately returns true:

if (array1Row != null && array2Row != null) {
    return true;
}

What you must do is this (completely invert the logic):

if (array1Row == null || array2Row == null) {
    return false;
}

这篇关于用于在多个二维数组中搜索相似条目的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆