【问题标题】:Code for searching similar entires in multiple two-dimensional arrays在多个二维数组中搜索相似条目的代码
【发布时间】:2012-02-06 01:55:01
【问题描述】:

我正在尝试为my previous topic 中描述的问题编写代码。建议的解决方案是使用哈希图在多个数组中查找相似的条目(数组具有相同的列数,但它们可能具有不同的行数)。

下面是我的示例代码,基于用户 John B 提供的代码 sn-p here。为简单起见和调试目的,我只创建了 3 个不同的一维行而不是二维数组。此外,为简单起见,函数 equalRows 应返回 truefalse 而不是行索引。

所以,在下面的代码中,函数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;
        }

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

}

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

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

【问题讨论】:

    标签: java arrays hashmap


    【解决方案1】:

    它只测试每个数组中的第一个元素并返回 true,因为它们匹配。

    如果有匹配失败则需要返回false,如果没有失败则返回true。

    我还会在 equalRows 方法的开头测试长度。

    【讨论】:

      【解决方案2】:

      此行错误,立即返回true:

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

      你必须做的是(完全颠倒逻辑):

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

      【讨论】:

      • 我用 int[] array1 = {1,2,3}; int[] array2 = {1,2,3}; int[] array3 = {1,2,3},虽然它应该是真的;由于某种原因,它返回 false。我认为问题出在函数 hashcode...(?)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2023-04-07
      相关资源
      最近更新 更多