我有一个用于排序我们先前使用插入排序方法创建的LinkedList的类。我们通过阅读Excel文件并列出5个贡献者来创建列表。我意识到这听起来像一个重复的问题......但是,我可以找到的所有样本处理整数或数组,我没有找到处理字符串或使用像我正在使用的LinkedList。另一个问题是,我发现处理不仅仅是整数的例子假设你使用头和节点以及类似的东西从头开始创建列表......正如你在我的代码中看到的那样,我没有使我的从头开始,我只是使用Java实用程序中的构建来制作我的。无论如何,我的代码可能并不是超高效的,但是到目前为止,我的任务已经达到了100个,所以对于我认为的学校来说这已经足够了,但是任何改进它的建议都是值得欢迎的。我是编程的初学者,只有经历我有以前的课程。所以,这里是我的代码:使用Java中的插入排序方法对链表排序
import java.io.*;
import java.util.*;
public class ChrisJohnson_Unit3_IP {
static class Contributor{ //create class to store contributor information
//declare variables
private String firstName;
private String lastName;
private String country;
private String phone;
private double contribution;
private int id;
//methods for setting variable values
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getCountry(){
return country;
}
public void setCountry(String country){
this.country = country;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone){
this.phone = phone;
}
public double getContribution(){
return contribution;
}
public void setContribution(double contribution){
this.contribution = contribution;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public void Print(){//method to print class objects
System.out.printf("%-10s %-10s %-8s %-15s %s %-15s %d %n", firstName, lastName, country,
phone, "$", contribution, id);
}
}//end Contributor class
static LinkedList contributorList = new LinkedList(); //create new Contributor Linked List
static Hashtable<String, Contributor> memberID = new Hashtable<>();//create new Hash Table
public static void main(String[] arg) throws Exception {
String response;
String ID;
Contributor contributorData = null;
Scanner in = new Scanner(System.in);
//print Welcome message and describe program to user
System.out.println("Welcome! This program will read your contributors.csv file "
+ "and store it into a list. \nTThe program will then sort the list and"
+ "print it for you to view/n");
System.out.println("Press enter to read the currently saved contributors.csv file...");
in.nextLine();
BufferedReader File =
new BufferedReader(new FileReader("contributors.csv"));
String dataRow = File.readLine(); // Read first line.
// The while checks to see if the data is null. If
// it is, end of file has been reached. If not,
// data will be processed.
while (dataRow != null){//While to read contributors.csv file and store in Contributor object
String[] data = dataRow.split(",");
contributorData = new Contributor(); //create new Contributor object
//store data into Contributor object
contributorData.setFirstName(data[0]);
contributorData.setLastName(data[1]);
contributorData.setCountry(data[2]);
contributorData.setPhone(data[3]);
contributorData.setContribution(Double.parseDouble(data[4]));
contributorData.setId(Integer.parseInt(data[5]));
ID = Integer.toString(contributorData.getId());
contributorList.push(contributorData);//add object to top of contributorList
memberID.put(ID,contributorData);//add contributor ID to key element of Hash Table
dataRow = File.readLine(); // Read next line of data.
}//end While to read contributors.csv file
File.close();//close CSV file
System.out.println("Here is your unsorted contributor list:\n");
//call Print method to print the list
System.out.printf("%-10s %-10s %-8s %-15s %-17s %s %n", "First", "Last",
"Country", "Phone #", "Contribution", "ID");
Iterator<Contributor> iter = contributorList.iterator();
while(iter.hasNext()){
iter.next().Print();
}//end while
System.out.println("Thank you for using this program!");
} //main()
}//end ChrisJohnson_Unit3_IP class
此外,列表必须使用插入排序方法按名称排序。我理解排序方法的基本概念,但老实说,我不知道如何在这里实现它。我不想找人帮我做作业,只是给我一个正确的方向。任何帮助将不胜感激,如果你需要更多的信息,请让我知道。这个任务是在星期一到期的,所以希望有人能够帮助我。是的,我已经写了我的老师寻求帮助,我整个星期都在城外,所以我一直在努力追赶。感谢您花时间阅读我的问题
为什么?这项任务是愚蠢的。只有疯子才会对链表进行排序。 – EJP
完全同意......这个班级完全被推迟,任务令人沮丧,因为现实世界中没有人会做我们正在学习的任何东西......但不幸的是,我必须这样做:/要离开一个当这个课程结束时,“很好”的评论... –