我正在尝试编写代码,以从文件中按字母顺序创建名称的链接列表。
姓名必须按姓氏排序,然后是名字。第二层排序是我遇到麻烦的地方。
到目前为止,我已经包含了我的代码,现在我只是想重写sortList
,以便它通过链表并在比较后添加一个节点。我写了一个sortList
以前使用的集合,它运行得很好,现在我试图在没有集合的情况下做到这一点。
我怀疑这比我做的要容易,而且我现在离代码太近了。下面是sortList
andcompareTo
方法...项目构造函数只包含firstName
and lastName
。
public int compareTo(linkedListProject arg0) {
if (this.lastName.equals(arg0.getLastName()))
return this.firstName.compareTo(arg0.getFirstName());
else
return this.lastName.compareTo(arg0.getLastName());
}
编辑(更改 sortList 以更准确地利用 compareTo)
public static void sortList(linkedListProject a)
{
Node temp = new Node(a);
if (head == null)
{head = temp;}
else {
Node cur = head;
while (cur.next == null && cur.data.compareTo(temp) > 0)
{ cur = cur.next;}
if (cur.next.data.compareTo(temp) <= 0)
temp = cur; }
}
再次编辑......基本上我意识到我必须检查节点并在我去的时候插入节点,所以这是我的完整代码。sortList 已重命名为 addToList。我仍然收到空指针异常
import java.util.*;
import java.io.*;
/**
* @author Michael Dangelo
* linkedListProject class, reads from file a provided classRoster then sorts them alphebetically by last name into the linkedList
*
*/
public class linkedListProject {
private String firstName;
private String lastName;
private static LinkedList<linkedListProject> classRoster = new LinkedList<linkedListProject>();
private static class Node
{
linkedListProject data;
private Node next = null;
private Node(linkedListProject data, Node next)
{
this.data = data;
this.next = next;
}
private Node(linkedListProject data)
{
this.data = data;
}
public linkedListProject getData()
{
return data;
}
}
private static Node head;
/**
* Default Constructor
*/
public linkedListProject()
{
head = null;
}
/**
* Constructor
* @param firstName First name of student
* @param lastName Last Name of Student
*/
public linkedListProject(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public void addToStart(linkedListProject data)
{
head = new Node(data, head);
}
/**
* Getter for firstName
* @return firstName
*/
public String getFirstName()
{
return firstName;
}
/**
* Setter for firstName
* @param firstName
*/
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
/**
* Getter for lastName
* @return lastName
*/
public String getLastName()
{
return lastName;
}
/**
* Setter for Last Name
* @param lastName
*/
public void setLastName(String lastName)
{
this.lastName = lastName;
}
/**
* toString method used for testing
*/
@Override
public String toString()
{
return "linkedListProject [firstName=" + firstName + ", lastName="
+ lastName + "]";
}
/**
* compareTo method used in sorting
*/
/**
* Prints out list to console
*/
public void printList()
{
int count = 0;
System.out.println("First Name: " + "Last Name: ");
while (count < classRoster.size())
{
linkedListProject a = classRoster.get(count);
System.out.println(a.getFirstName() + " \t" + a.getLastName());
count++;
}
}
/**
*
* @param a linked list ran in
*/
public static void addToList(linkedListProject a)
{
Node temp = head;
Node nextNode = null;
while (temp != null && nextNode.data.compareTo(a) >=0)
{
nextNode = temp;
temp = temp.next;
}
if (temp == null)
{
Node cur2 = new Node(a);
cur2.next = head;
head = cur2;
classRoster.add(cur2.getData());
}
else {
Node cur2 = new Node(a);
cur2.next = temp;
if (nextNode == null) {
nextNode.next = cur2;
classRoster.add(cur2.getData());
}
}
}
/**
* Reads in students names into object then adds and sorts list based on last name & firstName
* @param inputFileName The File Name
* @throws java.io.IOException
*/
public void read(String inputFileName) throws java.io.IOException
{
Scanner infile = new Scanner(new FileReader(inputFileName));
while(infile.hasNext())
{
String firstName = infile.next();
String lastName = infile.next();
linkedListProject intoList = new linkedListProject(firstName, lastName);
linkedListProject.addToList(intoList);
}
infile.close();
}
/**
* Driver
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
linkedListProject test = new linkedListProject();
test.read("179ClassList.txt");
test.printList();
}
public int compareTo(linkedListProject arg0)
{
if (this.lastName.equals(arg0.getLastName()))
return this.firstName.compareTo(arg0.getFirstName());
else
return this.lastName.compareTo(arg0.getLastName());
}
}