为什么if语句在for循环中不能正常工作?

时间:2015-06-10 13:43:37

标签: java arrays for-loop

我有一个数组按相同的值分类..

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

使用下面的代码很好,但是在for循环中的最后一个语句(if语句)不会打印。我不知道我的代码有什么问题

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

这是输出.. if语句在最后一个集群上不起作用(不打印)。 群集2(最后一个语句)不打印,它应该打印2,25,但为什么不打印任何东西?

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

2 个答案:

答案 0 :(得分:4)

在循环后打印换行符ArrayList以刷新输出缓冲区。

答案 1 :(得分:1)

你的两个$scope.expertise = [ {id: '1', level: 'Certified'}, {id: '2', level: 'Knowledgable'}, {id: '3', level: 'Interested'} ]; $scope.selection = []; $scope.$watch('filteredCaps | filter:{selected:true}', function (newValues) { $scope.selection = newValues.map(function (capname) { return {"capname" : capname.capname, "cid": capname.cid}; }); }, true); 都运行良好。只有一个是System.out.print(),另一个是System.out.println()

编辑: 我不确定我明白你在这里问的是什么。您的代码太模糊/很少,无法辨别问题。请告诉我们您的System.out.print()方法以及其他一些有用的代码。

相关问题