effecient algo

how do you compare values for a 2d array?
[48 byte] By [kat1016a] at [2007-10-3 5:22:53]
# 1
You really have to be more specific. A quick answer would be if... ;)Message edited for typos by: Martin@Stricent
Martin@Stricenta at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 2
e.g. i want to see if there are duplicate int values for every position or location in the array..
kat1016a at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 3
I smell Sudoku. :) It depends a little on what you need to do if you find duplicates. If you just have to find out that some exist (true or false): iterate over all elements, add each to a HashSet, and if its add() method returns false, you have a duplicate.
CeciNEstPasUnProgrammeura at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 4

hehe.. some kind of sudoku.. :D

ok let me elaborate further..

e.g int[][] a= new int[3][3];

this would look like..

a[0][0] a[0][1] a[0][2]

a[1][0] a[1][1] a[1][2]

a[2][0] a[2][1] a[2][2]

I would like to compare these values and at the same time get the coordinates of the locations which have the same values..

From the illustration, if a[j] have values,,,

1 2 3

3 4 5

6 7 8

I should get the coordiantes 0,2 and 1,0..

PS

Im such a newbie to Java.. :D And any help from you guys who I think are prof would be appreciated so much by me.. TY.

kat1016a at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi Kat..,

try this one,

public static void main(String[] args) {

int[][] a = new int[3][3];

a[0][0] = 1;

a[0][1] = 2;

a[0][2] = 3;

a[1][0] = 3;

a[1][1] = 4;

a[1][2] = 5;

a[2][0] = 6;

a[2][1] = 7;

a[2][2] = 8;

LinkedHashMap m = new LinkedHashMap();

for(int i = 0; i < 3; i++){

for(int j = 0; j < 3; j++){

if(m.containsKey(a[i][j])){

System.out.println("Duplicated indexes in coords: (" + i + "," + j + ")");

System.out.println("and (" + m.get(a[i][j]) + ")");

}else{

m.put(a[i][j], i + "," + j);

}

}

}

}

cheers,

mleiria

manuel.leiriaa at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 6
nope.. it's not working..
kat1016a at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 7
Well, it worked for me and the result was:Duplicated indexes in coords: (1,0)and (0,2)what did you get?mleiria
manuel.leiriaa at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 8
umm, I got a compiler error..hehe.. what version are you using?
kat1016a at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...
# 9
jdk1.5.0_07Can you post the compile error?
manuel.leiriaa at 2007-7-14 23:29:57 > top of Java-index,Java Essentials,Java Programming...