无法基于double属性对对象进行排序

时间:2014-02-21 14:14:14

标签: java bubble-sort

大家好我已经编写了一个KNN分类程序,我必须根据双变量的距离对对象进行排序,但我无法得到正确的输出。这是我的代码:

import java.util.*;
import java.io.*;

class Distance{
int index;
double distance;

public Distance(int index, double distance){
    this.index = index;
    this.distance = distance;
}
}

class KNN{

public static int getCount(){
    String file = "data2.txt";

    String line = null;

    int i = 0;

    try{
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        while((line=br.readLine())!=null){
            i++;
        }
        br.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
    i = i-1;
    return i; 
}

public static double[] setGenderValues(int k){
    double g[] = new double[k];
    String file = "data2.txt";
    String line = null;
    int i = 0;

    try{
        FileReader fr = new FileReader(file);

        BufferedReader br = new BufferedReader(fr);

        while((line=br.readLine())!=null){
            i++;
            if(i == 1)
            continue;

            String colData[] = line.split(" ");

            if(colData[1].equals("f")){
                g[i-2] = 1;
            }
            else if(colData[1].equals("m")){
                g[i-2] = 0;
            }
        }

        br.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
    return g;
}

public static double getMinHeight(double h[]){
    double min = h[0];
    for(int i = 1; i<h.length; i++){
        if(h[i] < min){
            min = h[i];
        }
    }
    return min;

}

public static double getMaxHeight(double h[]){
    double max = h[0];
    for(int i = 1; i<h.length; i++){
        if(h[i] > max){
            max = h[i];
        }
    }
    return max;
}

public static double[] setHeightValues(int k){
    String file = "data2.txt";
    double h[] = new double[k]; 
    String line = null;
    int i = 0;

    try{
        FileReader fr = new FileReader(file);

        BufferedReader br = new BufferedReader(fr);

        while((line=br.readLine())!=null){
            i++;
            if(i == 1)
            continue;
            String colData[] = line.split(" ");
            h[i-2] = Double.parseDouble(colData[2]);

        }


        br.close();
    }
    catch(Exception e){
        System.out.println(e);
    }

    return h;
}

public static double[] setClassValues(int k){
    String file = "data2.txt";

    String line = null;
    double c[] = new double[k];
    int i = 0;

    try{
        FileReader fr = new FileReader(file);

        BufferedReader br = new BufferedReader(fr);

        while((line=br.readLine())!=null){
            i++;
            if(i == 1)
            continue;
            String colData[] = line.split(" ");

            if(colData[3].equals("tall")){
                c[i-2] = 3;
            }
            else if(colData[3].equals("medium")){
                c[i-2] = 2;
            }
            else if(colData[3].equals("short")){
                c[i-2] = 1;
            }
        }


        br.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
    return c;
}

public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
    double u[] = new double[2];
    System.out.println("Enter gender; 1 for female, 0 for male");
    u[0] = scan.nextDouble();
    System.out.println("Enter height:");
    u[1] = scan.nextDouble();

    int k = getCount();

    //now, we normalize gender
    double g[] = setGenderValues(k);

    double h[] = setHeightValues(k);

    double vmin = getMinHeight(h);
    double vmax = getMaxHeight(h);

    //now, we normalize height
    for(int i = 0; i < k; i++){
        h[i] = (h[i] - vmin)/(vmax - vmin);
    }
    u[1] = (u[1] - vmin)/(vmax - vmin);

    //now, we set class values
    double c[] = setClassValues(k);

    /*System.out.println("gender height class");
    for(int i = 0;i< 10; i++){
        System.out.println(g[i]+" "+h[i]+" "+c[i]);
    }*/

    int n = (int)Math.sqrt(k);

    Distance d[] = new Distance[k];

    for(int i=0; i<k; i++){
        double distance = (u[0]-g[i])*(u[0]-g[i]) + (u[1]-h[i])*(u[1]-h[i]);
        distance = Math.sqrt(distance);
        d[i] = new Distance(i , distance);
    }

    for(int i =0; i<d.length; i++ ){
        System.out.println(d[i].index + " " + d[i].distance);
    }

    Distance temp;
    for(int i=0; i<k-1; i++){
        for(int j=0; j<k-1; j++){
            if(d[j].distance > d[j+1].distance){
                temp = d[j];
                d[j] = d[j+1];
                d[j+1] = d[j];
            }
        }

    }

    int count_tall = 0, count_short = 0, count_medium = 0;
    System.out.println("sorted:");
    for(int i =0; i<d.length; i++ ){
        System.out.println(d[i].index + " " + d[i].distance);
    }
}
}

这是我的文字档案:     person_id性别高度等级     1 f 1.6短     2米2.0高     3米1.85中等     4 f 1.9中等     5米1.7短     6 f 1.8中等     7 f 1.95中等     8 f 1.75中等     9米2.2高     10米2.1高

1 个答案:

答案 0 :(得分:0)

阅读Javadoc on the Comparable interface。如果你不知道界面是什么,谷歌吧。

class Distance后添加implements Comparable

为您的班级添加两种方法。一个将boolean compareTo(Distance d) {}遵循Javadoc中Comparable的说明。另一个是boolean equals(Distance d) {}。您需要添加的是用于比较双倍的逻辑。

现在,您的对象可以与><==进行比较。它们也可以由Collections类以前所未有的方式进行排序和操作。

我会给你一个提示 - 你compareTo()的逻辑是return this.distance.compareTo(d.distance);