简体   繁体   English

Java比较二维数组

[英]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] dok1 [paragraf] [kalimat]
  • dok2[paragraf][kalimat] dok2 [paragraf] [kalimat]

*dok1 * 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 * 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, 我尝试使用4个嵌套循环,

    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. 可以清楚地看到,dok2中的kalimat数组元素保持为0。它们甚至没有递增。 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: 如果您自己不需要索引(只需每个数组元素的String值),则可以使用增强的for循环以更简单的代码完成相同的循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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