我的类(class)有一个作业,要对我们之前使用插入排序方法创建的 LinkedList 进行排序。我们通过读取列出了 5 个贡献者的 Excel 文件来创建该列表。我意识到这听起来像是一个重复的问题...但是,我能找到的所有示例都涉及整数或数组,我找不到任何涉及字符串或像我正在使用的 LinkedList 的示例。另一个问题,我确实发现处理不仅仅是整数的示例假设您使用 Head 和 Node 以及类似的东西“从头开始”创建列表......正如您在我的代码中看到的那样,我没有制作我的列表从头开始,我只是使用 Java 实用程序中的构建来制作我的。无论如何,我的代码可能不是 super 高效,但到目前为止我的每项作业都得到了 100 分,所以我想这对于学校来说已经足够好了,但也欢迎任何改进的建议。我是编程初学者,我唯一的经验就是以前的类(class)。所以,这是我的代码:
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
同样,列表必须使用插入排序方法按名称排序。我理解排序方法的基本概念,但老实说不知道如何在这里实现它。我不是在找人帮我做作业,只是在正确的方向上插入我。任何帮助将不胜感激,如果您需要更多信息,请告诉我。这项作业将于周一截止,所以希望届时有人能够帮助我。是的,我已经写信给我的教练寻求帮助,我整个星期都在外地,所以我一直在努力追赶。感谢您花时间阅读我的问题
最佳答案
首先,我应该说,在链表上进行插入排序是完全没有意义的。其次,如果您添加一个连接贡献者名字和姓氏的 getName 方法,您可以执行如下操作(您可以在排序时连接,但您的代码会更困惑)。
for( int i = 1; i < contributorList.size(); i++)
{
int j = i;
Contributor tmp;
while( j > 0 && contributorList.get(j-1).getName().compareTo( contributorList.get(j).getName()) > 0)
{
tmp = contributorList.remove( j);
contributorList.add( j-1, tmp);
j = j - 1;
}
}
关于java - Java中使用插入排序方法对链表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39305928/