简体   繁体   中英

Java Comparing Two Dimensional Arrays

I really need help in comparing two dimensional arrays. In case I have the following values of arrays:

  • dok1[paragraf][kalimat]
  • dok2[paragraf][kalimat]

*dok1

[0][0] = Deskripsi Hotel

[1][0] = PETIK HOTEL


[2][0] = Pelayanan yang ramah juga diberikan di hotel ini


[2][1] = Lembang no 24


[2][2] = hotel ini berdiri pada tahun 1994

[3][0] = Gambar Template/layout pada website ini

*dok2

[0][0] = Banyak penjual kerudung di jalan keluar pabrik

[1][0] = Di sisi-sisi penjual itu juga nampak seperti penjual kurma dan buah dadakan

[1][1] = Penjual makanan juga memenuhi trotoar jalan, yang sebagian besar adalah penjual dadakan

[1][2] = Mulai dari menu takjil hingga makanan berbuka puasa tersedia disini



[2][0] = Belum lagi penjual pakaian yang memanjang hingga ujung jalan

[3][0] = Kerumunan pedagang tersebut makin membuat lalu lintas padat

I need to compare them in order to count the similarity between the string elements. It should be like this:

[0][0] vs [0][0]

[0][0] vs [1][0]

[0][0] vs [1][1]

[0][0] vs [1][2]

[0][0] vs [2][0]

[0][0] vs [3][0]

and so on.

I've tried to use the 4 nested loops,

    for (int i = 0; i < dok1.length; i++) {
                for (int j = 0; j < dok1[i].length; j++) {
                    for (int k = 0; k < dok2.length; k++) {
                        for (int l = 0; l < dok2[k].length; l++) {

but the result is as the following:

[0][0] vs [0][0]

[0][0] vs [1][0]

[0][0] vs [2][0]

[0][0] vs [3][0]

[1][0] vs [0][0]

[1][0] vs [1][0]

[1][0] vs [2][0]

[1][0] vs [3][0]

and so on.

It's clearly seen that the array elements of kalimat in dok2 remains 0. They're not even incrementing. Am I doing something wrong in looping method? Does anybody have a better approach? Thanks.. :)

You have to walk the two arrays both at the same time, not in four nested loops.

assert dok1.length == dok2.length
int firstDimension = dok1.length;

for (int i = 0; i < firstDimension; i++) {
    assert dok1[i].length = dok2[i].length;
    int secondDimension = dok1[i].length;

    for (int j = 0; j < secondDimension; j++) {
        // Comparing
    }
}
            int similarCounter=0;
            for(int i=0;i<paragraf;i++){//paragraf is dok1.length
                for (int j = 0; j < kalimat; j++) {//kalimat is dok1.width
                    if((dok1[i][j]).equals(dok2[i][j])){//replace it with your codition of comparison 
                        System.out.println("Similar ("+i+","+j+")");
                        similarCounter++;
                    }
                }
            }
            System.out.println("There are tolally "+similarCounter+" similar!");

I suspect that your original loops were correct but that you have a subscripting error in the body of the innermost loop. However, your loops are inefficient because you are repeatedly evaluating array access expressions in the inner loops that are constant for each iteration of the outer loops. You can speed up the code (and minimize the risk of subscripting errors) with some temporary variables:

for (int i = 0; i < dok1.length; i++) {
    final String[] row1 = dok1[i];
    for (int j = 0; j < row1.length; j++) {
        final String item1 = row1[j];
        for (int k = 0; k < dok2.length; k++) {
            final String[] row2 = dok2[k];
            for (int l = 0; l < row2.length; l++) {
                final String item2 = row2[l];
                // compare item1 (== dok1[i][j]) with item2 (== dok2[k][l])
            }
        }
    }
}

If you don't need the indexes themselves (just the String value at each array element), you can use an enhanced for loop to accomplish the same looping with simpler code:

for (final String[] row1 : dok1) {
    for (final String item1 : row1) {
        for (final String[] row2 : dok2) {
            for (final String item2 : row2) {
                // compare item1 with item2
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM