为什么我的排序功能会产生如此不寻常的输出?

时间:2014-02-16 21:17:26

标签: java class sorting comparator comparable

我有一个程序从csv文件中读取,这些文件中包含各种姓氏,名字和出生年份,将它们分配到一个特殊的类数组中,然后根据它们的姓氏进行排序。我相信我的代码是有效的,所以我需要做的就是输出清单,看看是否确实所有的人都按照他们的姓氏排序。但是,我无法找到正确的语法来执行此操作。 这是我的Main.java的代码,我认为问题必须在这里。

package project_1_sorting;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



public class Main 

{

    public static void main(String[] args) throws IOException {
        // open file input stream
        BufferedReader reader = new BufferedReader(new FileReader(
                "C:\\Users\\Owner\\Desktop\\Data 18k.csv")); // double check where this is trying to read it from

        // read file line by line
        String line = null;
        Scanner scanner = null;
        int index = 0;
        Human[] pplArray = new Human[18000];
                int i = 0;
        while ((line = reader.readLine()) != null) {
            Human ppl = new Human();
            scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                String data = scanner.next();
                if (index == 0)
                    ppl.setLastName(data);
                else if (index == 1)
                    ppl.setFirstName(data);
                else if (index == 2)
                    ppl.setBirthYear(data);
                else
                    System.out.println("invalid data::" + data);
                index++;
            }
                        ppl.setKey(0); //change this for later things, you can use loop
                        ppl.setOrder(0); //change this to 1 if you want to invert the list of people
            index = 0;
            pplArray[i] = ppl;
                        i++;
                        System.out.println(pplArray);
        }
        //close reader
        reader.close();

        System.out.println(pplArray); // create


           Selection_Sort selection = new Selection_Sort();   


            for (int j = 0; j < 18000; j++)
     {
         System.out.println(pplArray[j]);
     }
    }

}

所以我希望这能从csv文件中输出我所有人的巨大列表(已订购),所有信息都与原来的格式相同,对。 (每行一个人,3个字符串有3个字段)。然而,这就是我所得到的:

run:
Test
17
true
0.142857
BUILD SUCCESSFUL (total time: 0 seconds)

我注意到的一件事是最后一行读取

Selection_Sort selection = new Selection_Sort();

它说“不使用变量选择”。 我认为这是说我没有正确使用我的选择排序。我正在创建该类的对象,但不使用它。

这是我的Selection_Sort.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package project_1_sorting;

public class Selection_Sort 
{
    public static void sort(Comparable[] a)
    { // Sort a[] into increasing order.
        int N = a.length; // array length
        for (int i = 0; i < N; i++)
        { // Exchange a[i] with smallest entry in a[i+1...N).
            int min = i; // index of minimal entr.
            for (int j = i+1; j < N; j++)
                if (less(a[j], a[min])) min = j;
            exch(a, i, min);
        }
    }
// See page 245 for less(), exch(), isSorted(), and main()

private static boolean less(Comparable v, Comparable w)
    { 
    return v.compareTo(w) < 0; 
    }
private static void exch(Comparable[] a, int i, int j)
    { 
    Comparable t = a[i]; a[i] = a[j]; a[j] = t; 
    }

}

我认为,为了按照正确的顺序从csv文件中实际排序我的数据。我必须对

的影响做些什么
 Selection_Sort selection = new Selection_Sort();   
 selection.sort(pplArray[]);

但是,第二行代码会创建一条错误消息:

'.class' expected
 cannot find symbol
 symbol: class pplArray
 location : class main

如果我删除了pplArray上的括号:

 Selection_Sort selection = new Selection_Sort();   
 selection.sort(pplArray);

错误消息更改为

incompatible types: Human[] cannot be converted to Comparable

这让我想知道我的Human类是否有问题,所以这是我的Human.java:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package project_1_sorting;



class Human{
    String firstName;
    String lastName;
    String birthYear;
    int Key;
    int Order;
    public Human() // A constructor
    { 
    }

public int compareTo(Human person)
{
    if (Order == 0)
    {
      if (Key == 0)       
            return lastName.compareTo(person.lastName );
     else if  (Key == 1)    
            return firstName.compareTo(person.firstName );
     else 
            return birthYear.compareTo(person.birthYear );
    }
    else
            {
    if (Key == 0)       
            return (lastName.compareTo(person.lastName ) ) * -1;
    else if  (Key == 1)    
            return ( firstName.compareTo(person.firstName ) ) * -1;
    else 
            return ( birthYear.compareTo(person.birthYear ) ) * -1;
    }
}

public void printHuman ()
{
    StdOut.print(lastName + " " + firstName + " " + birthYear);
}

public void setKey (int k)
{
    Key = k;
}

public void setOrder (int o)
{
    Order = o;
}

public void setFirstName(String fName)
{
     firstName = fName;
}

public void setLastName(String lName)
{
     lastName = lName;
}

public void setBirthYear(String bYear)
{
     birthYear = bYear;
}

 }

如果它是相关的,int key应该允许我选择我想要排序的变量(名字,姓氏或出生年份),并且int order应该允许我颠倒顺序哪些人被订购(最少到最大,最大到最少)。

如果我不能使用可比较的,这是否意味着我必须使用比较器?如果是这样,有人可以告诉我如何做到这一点吗?

如果有人知道这是什么,请告诉我。如果这个Main.java没有其他问题,我可以发布我的其他.java文件。

我注意到的一件事是,即使我注释了我的选择排序函数调用,以及此.java文件中的所有printline命令,我的屏幕上也会显示相同的输出。

请让我知道你的想法,谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

你走在正确的轨道上。但是,编译器如何知道您的Human类是Comparable?您不仅必须提供方法compareTo(),还要将implements Comparable放在开头。

答案 1 :(得分:0)

您的Human类似乎没问题 - 但是虽然您已经实现了compareTo()函数,但您还没有声明您的类实现了Comparable接口(这是您的编译错误)得到):

class Human implements Comparable<Human> {

然后,您可能需要注释compareTo()函数:

@Override
public int compareTo(Human person)