我有在比较双打的问题。 比较负责停止while循环。 该代码运行正常,但突然,循环永远不会停止。 从centroidList的dimVal的值被比较,以由此计算的临时变量(粗体位)。 该代码总是进入,如果,如果我用也没关系“!=”或“==”。 打印出来的价值,它们是完全一样的。 怎么了?
package clusters;
import java.util.LinkedList;
public class KMeansV2 {
LinkedList<Record> table;
LinkedList<Centroid> centroidList;
LinkedList<Double> intervalList;
boolean clusterStop;
int meassureType;
int prec=10000000;
KMeansV2()
{
Read read=new Read(true,"BrCa_HD_full.xlsx");
table=new LinkedList<Record>(read.table);
centroidList=new LinkedList<Centroid>();
CreateCentroids(2);
SetMeassureType(1);
while(clusterStop==false)
{
UpdateRecords();
UpdateClusters();
}
Output();
}
public void SetMeassureType(int meassureType)
{
this.meassureType=meassureType;
}
public void CreateCentroids(int centroidCount)
{
if(centroidList.isEmpty())
{
for(int i=0;i<centroidCount;i++)
{
centroidList.add(new Centroid(table.get(0).values.size(),i));
}
}
else
{
centroidList.clear();
for(int i=0;i<centroidCount;i++)
{
centroidList.add(new Centroid(table.get(0).values.size(),i));
}
}
}
public void UpdateRecords()
{
for(int i=0;i<table.size();i++)
{
table.get(i).Update(centroidList, meassureType);
}
}
public void UpdateClusters()
{
clusterStop=true;
for(int i=0;i<centroidList.size();i++) //staiga pa centroidiem
{
for(int j=0;j<table.get(0).values.size();j++) //staiga pa kolonnam
{
double sum=0;
double count=0;
for(int k=0;k<table.size();k++) //staiga pa rindam
{
if(centroidList.get(i).type==table.get(k).type)
{
sum+=table.get(k).values.get(j);
count++;
}
}
System.out.println(clusterStop);
double temp=(1/count)*sum;
**if(centroidList.get(i).dimVal.get(j)==temp);
{
System.out.println(centroidList.get(i).dimVal.get(j)+" != "+(1/count)*sum);
clusterStop=false;
}**
centroidList.get(i).dimVal.set(j,temp);
}
System.out.println(clusterStop);
}
}
public void Output()
{
LinkedList<String> types=new LinkedList<String>();
for(int i=0;i<table.size();i++)
{
if(!types.contains(table.get(i).realType))
{
types.add(table.get(i).realType);
}
}
for(int i=0;i<centroidList.size();i++) //staiga pa centroidiem
{
for(int j=0;j<types.size();j++) //staiga pa klasem
{
int count=0;
for(int k=0;k<table.size();k++) // staiga pa rindam
{
if(table.get(k).type==i && table.get(k).realType.equals(types.get(j)))
{
count++;
}
}
System.out.println("Centroid "+(i+1)+" has "+count+" of type "+types.get(j));
//kMeansUI.UpdateLog("Centroid "+(i+1)+" has "+count+" of type "+types.get(j));
}
}
for(int i=0;i<centroidList.size();i++)
{
int count=0;
for(int j=0;j<table.size();j++)
{
if(table.get(j).type==i)
{
count++;
}
}
System.out.println("Cluster "+i+" has "+count+" records.");
//kMeansUI.UpdateLog("Cluster "+(i+1)+" has "+count+" records.");
}
//kMeansUI.UpdateLog("/-------------------------------------------------------------------------/");
}
public static void main(String[] args)
{
KMeansV2 test=new KMeansV2();
}
}