Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 2.08 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Code for searching similar entires in multiple two-dimensional arrays
  2. import java.util.HashMap;
  3. import java.util.Map;
  4.  
  5. public class Test {
  6.  
  7.     public static void main(String[] args) {
  8.         int[] array1 = {1,2,3};
  9.         int[] array2 = {1,2,3};
  10.         int[] array3 = {1,3,4};
  11.         boolean answ = equalRows(array1,array2,array3);
  12.         System.out.println(answ);
  13.     }
  14.  
  15.     static class Row extends Object {
  16.         private int value;
  17.         private volatile int hashCode = 0;
  18.  
  19.         public Row(int val) {
  20.             this.value = val;
  21.         }
  22.  
  23.         @Override
  24.         public boolean equals(Object obj) {
  25.             if(this == obj)
  26.                 return true;
  27.             if((obj == null) || (obj.getClass() != this.getClass()))
  28.                 return false;
  29.             // object must be Row at this point
  30.             Row row = (Row)obj;
  31.                 return (value == row.value);
  32.         }
  33.  
  34.         @Override
  35.         public int hashCode () {
  36.             final int multiplier = 7;
  37.             if (hashCode == 0) {
  38.                 int code = 31;
  39.                 code = multiplier * code + value;
  40.                 hashCode = code;
  41.             }
  42.             return hashCode;
  43.         }
  44.     }
  45.  
  46.     private static Map<Row, Integer> map(int[] array) {
  47.           Map<Row, Integer> arrayMap = new HashMap<Row, Integer>();
  48.           for (int i=0; i<array.length; i++)
  49.                 arrayMap.put(new Row(array[i]), i);
  50.           return arrayMap;
  51.     }
  52.  
  53.     private static boolean equalRows(int[] array1, int[] array2, int[] array3){
  54.            Map<Row, Integer> map1 = map(array1);
  55.            Map<Row, Integer> map2 = map(array2);
  56.  
  57.            for (int i=0; i<array3.length; i++){
  58.               Row row = new Row(array3[i]);
  59.               Integer array1Row = map1.get(row);
  60.               Integer array2Row = map2.get(row);
  61.               if (array1Row != null || array2Row != null) {
  62.                   return false;
  63.               }
  64.            }
  65.         return true;
  66.     }
  67.  
  68. }
  69.        
  70. if (array1Row != null && array2Row != null) {
  71.     return true;
  72. }
  73.        
  74. if (array1Row == null || array2Row == null) {
  75.     return false;
  76. }