gpt4 book ai didi

java - 为什么我的排序函数会产生如此不寻常的输出?

转载 作者:行者123 更新时间:2023-12-01 13:25:44 26 4
gpt4 key购买 nike

我有一个程序,它从一个包含人们姓氏、名字和出生年份的 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 文件中的所有打印行命令,屏幕上也会显示相同的输出。

请告诉我您的想法,感谢您的宝贵时间。

最佳答案

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

关于java - 为什么我的排序函数会产生如此不寻常的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21816764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com