
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 2.08 KB | hits: 11 | expires: Never
Code for searching similar entires in multiple two-dimensional arrays
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;
}
}
if (array1Row != null && array2Row != null) {
return true;
}
if (array1Row == null || array2Row == null) {
return false;
}