简体   繁体   中英

Why does if statement not work properly inside for loop?

I have an array to be classified by same value..

int[] clusters= 0    1    2    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1
int[] clusterDistinct = 0 1 2

It is fine using code below, but on last statement (if statement) inside for loop does not print. I don't know whats wrong with my code

private void autoClustering(int[] clusters) {
    int[] clusterDistinct = getClusters(clusters);

    for (int i = 0; i < clusterDistinct.length; i++) {
        System.out.println("\nCluster " + clusterDistinct[i]);
        for (int j = 0; j < clusters.length; j++) {
            if (clusters[j] == clusterDistinct[i]){
                System.out.print(j+",");
            }
        }
    }
}

private int[] getClusters(int[] clusters) {
        ArrayList<Integer> klaster = new ArrayList<Integer>();
        for (int i = 0; i < clusters.length; i++) {
            boolean isDistinct = false;
            for (int j = 0; j < i; j++) {
                if (clusters[i] == clusters[j]) {
                    isDistinct = true;
                    break;
                }
            }
            if (!isDistinct) {
                klaster.add(clusters[i]);
            }
        }

        int[] clusterDistinct = new int[klaster.size()];
        for (int i = 0; i < clusterDistinct.length; i++)
            clusterDistinct[i] = klaster.get(i).intValue();
        return clusterDistinct;
    }

Here is output.. if statement does not working (not print) on last cluster. cluster 2 (last statement) not print, it should print 2,25 but why not print anything?

06-10 20:38:34.204: I/System.out(10634): Cluster 0:
06-10 20:38:34.204: I/System.out(10634): 0,
06-10 20:38:34.204: I/System.out(10634): Cluster 1:
06-10 20:38:34.204: I/System.out(10634): 1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
06-10 20:38:34.204: I/System.out(10634): Cluster 2:
06-10 20:38:34.204: D/BRCM_EGL(10634): eglMakeCurrent(NULL) Thread: 10634
06-10 20:38:34.204: D/BRCM_EGL(10634): eglDestroySurface() surface: 0x4d4beb30, android window 0x4d4be420, Thread: 10634

在循环之后输出换行符\\n刷新输出缓冲区。

Both of your System.out.print() s are working fine. Just one is a System.out.println() and the other a System.out.print() .

Edit: I am not sure I understand what you are asking here. Your code is too vague/little to discern the issue. Show us your getClusters method as well as some other useful code please.

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