在3二维数组找到类似的行 [英] Find similar rows in 3 two-dimensional arrays

查看:268
本文介绍了在3二维数组找到类似的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是解决Java的以下问题的最佳方法?
它有行和列的相等数目3的二维数组:

What is the best approach to solve the following problem in Java? There are 3 two-dimensional arrays that have equal number of rows and columns:

Array1:
[1]: {1, 2, 3}
[2]: {2, 2, 4}
[3]: {1, 1, 1}

Array2:
[1]: {1, 2, 0}
[2]: {1, 2, 3}
[3]: {1, 0, 1}

Array3:
[1]: {1, 1, 1}
[2]: {1, 2, 3}
[3]: {1, 0, 1}

我需要找出的存在于所有的数组行的索引。在上面的例子中,答案应该是:<强> [1],[2],[2]

I need to find out the indexes of rows that exist in all arrays. In the above example the answer should be: [1],[2],[2]

数组1
[1] :{1,2,3}

Array1 [1]: {1, 2, 3}

ARRAY2:
[2] :{1,2,3}

Array2: [2]: {1, 2, 3}

ARRAY3:
[2] :{1,2,3}

Array3: [2]: {1, 2, 3}

更新:有没有内置的功能来做到这一点?或者是FOR循环的独特的解决方案?

UPDATE: Is there any built-in function to do this? Or is the FOR loop the unique solution?

推荐答案

我要做到以下几点:


  • 创建一个类,需要一个排的阵列并实施散code 等于

  • 将每一行(包裹在上面的类)中前两个阵列分成两个 HashMaps这样

  • 在过去的数组中的每一行,确定他们是否在 HashMaps这样
  • 存在
  • Create a class that takes a row in the array and implements hashcode and equals.
  • Insert each row (wrapped in the above class) in the first two arrays into two HashMaps
  • for each row in the last array, determine if they exist in the HashMaps

编辑#2,实现了测绘需要得到扭转。

Edit #2, realized the mapping would need to be reversed.

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

private mySearch(){
   int[] array1, array2, array3;

   Map<MyClass, Integer> map1 = map(array1);
   Map<MyClass, Integer> map2 = map(array2);

   for (int i=0; i<array3.lenght; i++){
      MyClass row = new MyClass(array3[i]);
      Integer array1Row = map1.get(row);
      Integer array2Row = map2.get(row);

      if (array1Row != null && array2Row != null){
         // Matching rows found
      }
   }
}

这篇关于在3二维数组找到类似的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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