我有一个数组要按相同的值进行分类..
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 语句在最后一个集群上不起作用(不打印)。
cluster 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
最佳答案
在循环后打印换行符 \n
以刷新输出缓冲区。
关于java - 为什么 if 语句在 for 循环中不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758171/