0

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.. :)

2
  • How do you want to measure the similarity between elements? How does that turn into a comparison between the arrays themselves? How do you want to represent the results? Do you want to compare corresponding elements or do you want to compare each element of one array with all elements of the other? Commented Feb 25, 2014 at 7:45
  • I use a my own method using Levenshtein distance algorithm. But that's not the thing I concern in this question. All I want is how to compare each element of one array with all elements of the other. No need to use any condition such as if or if-else. Just print out all the process so I can see how the process is going. If you can provide such method, I would be very grateful.. :) Commented Feb 25, 2014 at 9:00

3 Answers 3

1

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
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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
    }
}

1 Comment

The result is not as I expected. But thanks for your response.
1
            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!");

1 Comment

The result is not as I expected. But thanks for your response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.