gpt4 book ai didi

java - 如何将文本文件的内容保存到链表中?

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:11 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,将文本文件的内容保存到单链表中,txt文件中的每一行都是一个节点。我使用扫描仪读取文件,但无法将文本放入链接列表中。 我创建了一个链表类和一个 Node 类。 任何提示或帮助都会有帮助。
这是我的链接列表类:

        import java.util.Iterator;

public class LinkedList<T> { //Generic
private Node<T> myStart;
private Node<T> myEnd;
private int mySize;

public LinkedList(){
mySize = 0;
myStart = null;
myEnd = null;
}


// Clears the List
public void clear(){
myStart = null;
myEnd = null;
mySize = 0;
}

public T getFirst(){
if (myStart == null){
return null;
}
return myStart.getValue();
}


public boolean isEmpty() {
// Returns ONLY and ONLY IF "myElements" equals to 0
return mySize == 0;
}

public int size() {
return mySize;
}

//Add a value to the list
public void add(T aValue ){
Node<T> theNode = new Node<T>(aValue);
theNode.setLink(myStart);
myStart = theNode;
myEnd = theNode;
mySize++;
}

//Adds a value to the end of the List
public void addToEnd(T aValue){
Node<T> theNode = new Node<T>(aValue);
if(myEnd == null){
myStart = theNode;
myEnd = theNode;
}
else {
myEnd.setLink(theNode);
myEnd = theNode;
}
mySize++;
}

//Removes a value from the list
public void remove(T aValue){
Node<T> current = myStart;
Node<T> previous = null;
while(current !=null){
if(current.getValue().equals(aValue)){
mySize--;
if (previous == null){
myStart = current.getLink();
}
else{
previous.setLink( current.getLink() );
}
}
if(current == myEnd){
myEnd = previous;
}
previous = current;
current= current.getLink();
}
return;
}

//Prints the current list
public void print(){
Node<T> current = myStart;
while(current !=null){
System.out.print(current.toString() + " ");
current= current.getLink();
}
System.out.println();
}

}

然后我尝试读取我可以读取的文件,但我不知道为什么它不能正确打印列表。这是我将文件添加到链接列表中的位置:

 public class SpellCheck<T> {

public SpellCheck(){

}

//LoadData reads the file and places it in a linkedList
public static String loadData(String fileName){
LinkedList<String> dictionary = new LinkedList<String>(); //Create a new LinkedList called dictionary
Scanner scan = null;
try {
scan = new Scanner(new FileInputStream(fileName)); //read in the file
}
catch (FileNotFoundException e) {
e.printStackTrace();
}

while(scan.hasNext()) {
String theList = scan.nextLine();
dictionary.add(theList); //Add theList to the LinkedList "Dictionary"
System.out.println( dictionary ); //print it out
}
scan.close();
}
}

我的文本文件包含随机单词,例如:惊讶后面放弃弃放弃放弃篮球篮子晒太阳巴斯克低音

在我的测试驱动程序中,这就是我在 TestDriver 类中所做的事情: 公共(public)类测试驱动程序{

    public static void main(String[] args) {
LinkedList<String> theList = new LinkedList<String>();
theList.add(SpellCheck.loadDataLinkedList("standard.txt"));
theList.print();
}
}

最佳答案

我建议您采用自上而下的方法,并使用标准类。 (重新发明轮子可能很有趣,但有时您最好使用 java.util 中久经考验的轮子。)

考虑:

public LinkesList<String> readLines(BufferedReader source) {
LinkedList<String> result = new LinkedList<>();
while (true) {
String line = source.readLine();
if (line == null) break; // end of file
result.add(line);
}
return result;
}

LinkedList<String> loadFile(Sting filename) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
return readLines(reader);
} finally {
// free up the file, exception or not.
// yes, this happens before the return, too.
if (reader != null) reader.close();
}
}

打印列表(事实上,任何可迭代的列表)也非常简单:

List<String> lines = ...;
for (String item : lines) {
System.out.println(item);
}

关于java - 如何将文本文件的内容保存到链表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29703740/

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