【发布时间】:2014-02-16 21:17:26
【问题描述】:
我有一个程序,它从一个包含人们姓氏、名字和出生年份的 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 应该允许我选择我想要排序的变量(first name、last name 或出生年份),并且 int order 应该允许我颠倒顺序哪些人是按顺序排列的(从小到大,从大到小)。
如果我不能使用比较器,是否意味着我必须使用比较器?如果是这样,有人可以告诉我怎么做吗?
如果有人知道这是怎么回事,请告诉我。如果这个 Main.java 没有其他问题,我可以发布我的其他 .java 文件。
我注意到的一件事是,即使我注释掉了我的选择排序函数调用,以及此 .java 文件中的所有打印行命令,我的屏幕上仍会显示相同的输出。
请告诉我您的想法,感谢您抽出宝贵时间。
【问题讨论】:
标签: java class sorting comparator comparable