[英]reading lines in from a text file and sorting them into a linked list java
[英]What is a better method to sort strings alphabetically in a linked list that is reading in lines from a text file?
public class ContactList {
private ContactNode head;
private ContactNode last;
public ContactNode current;
public ContactList value;
public ContactList() {}
public void addNode(ContactNode input) {
if (this.head == null) {
this.head = input;
this.last = input;
} else last.setNext(input);
input.setPrev(last);
this.last = input;
}
public void traverse() {
System.out.println();
current = this.head;
while (current != null) {
System.out.print(current.getName() + " ");
System.out.println("");
current = current.getNext();
}
System.out.println();
}
public void insertNewFirstNode(String value) {
ContactNode newNode = new ContactNode(value);
head = newNode;
if (last == null) {
last = head;
}
}
public void sort() {
ContactList sorted = new ContactList();
current = head;
while (current != null) {
int index = 0;
if ((current.getName() != null)) {
index = this.current.getName().compareTo(current.getName());
if (index == 1) {
sorted.insertNewFirstNode(current.getName());
}
current = current.getNext();
} else if ((current != null)) {
System.out.print(sorted + "\n");
}
}
} // end contactList
主要方法:
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class ContactMain {
public static void main(String[] args) {
try {
FileReader filepath = new FileReader("data1.txt");
Scanner k = new Scanner(filepath);
ContactList myList = new ContactList();
while (k.hasNextLine()) {
String i = k.nextLine();
myList.addNode(new ContactNode(i));
}
myList.traverse();
myList.sort();
myList.traverse();
} catch (FileNotFoundException e) {
System.out.println("File Not Found. ");
}
}
}
節點類別:
public class ContactNode {
private String name;
public int index;
private ContactNode prev;
public ContactNode next;
ContactNode(String a) {
name = a;
index = 0;
next = null;
prev = null;
}
public ContactNode getNext() {
return next;
}
public ContactNode getPrev() {
return prev;
}
public String getName() {
return name;
}
public int getIndex() {
return index;
}
public void setNext(ContactNode newnext) {
next = newnext;
}
public void setPrev(ContactNode newprevious) {
prev = newprevious;
}
public void setName(String a) {
name = a;
}
public void setIndex(int b) {
index = b;
}
}
我正在制作一個有趣的程序,該程序從文本文件中讀取聯系人信息,並將其放入鏈接列表中。 我想創建一個sort()
方法來按字母順序對每個節點或名稱進行排序。 我已經做了大量的研究,並且我的方法只打印諸如ContactList@282c0dbe
類的代碼,其行數與我的文本文件一樣多。
您需要自定義Comparator
進行排序 ,並且要漂亮地打印List
您需要在ContactList
類中實現toString()
什么是ContactList@282c0dbe
?
它是類名稱,后跟符號和哈希碼,然后是對象的哈希碼。Java中的所有類都直接或間接地繼承自Object類。 Object類具有一些基本方法,例如clone(),toString(),equals()等toString()
的默認toString()
方法打印“class name @ hash code”.
有什么解決辦法 ?
您需要在ContactList
類中重寫toString
方法,因為它將以您可以理解的可讀格式為您提供有關對象的清晰信息。
覆蓋toString
的優點:
幫助程序員記錄和調試Java程序
由於toString是在java.lang.Object中定義的,並且不會提供有價值的信息,因此,對於子類覆蓋它是一個很好的實踐。
@override
public String toString(){
// I assume name is the only field in class test
return name + " " + index;
}
對於排序,您應該實現Comparator接口,因為您的對象沒有自然排序 。 從更好的意義上來說,如果要定義外部可控制的訂購行為,則可以覆蓋默認的訂購行為
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.