此任務的目標是將單詞列表從文件輸出到單個鏈接列表中,然後按字母順序排序。但我無法弄清楚如何將單個單詞放入鏈表中。我對此非常感興趣,任何提示或提示都將非常感謝。如何將單詞從文本文件轉換爲單向鏈接列表
這裏是我Node
類我相信這是正確的:
//Node of a singly linked list of strings
public class Node {
private String element;
private Node next;
//creates a node with the given element and next
public Node(String s, Node n){
element = s;
next = n;
}
//Returns the elements of this
public String getElement(){
return element;
}
public Node getNext(){
return next;
}
//Modifier
//Sets the element of this
public void setElement(String newElement){
element = newElement;
}
//Sets the next node of this
public void setNext(Node newNext){
next = newNext;
}
}
這裏是從文件採取的句子,將其分解到個人的話我的主類。這就是被我有一個問題,我無法弄清楚如何讓個人的話到鏈表:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class DictionaryTester{
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("input1"));
String file;
int lineNum = 1;
while ((file = br.readLine()) != null) {
System.out.print("(" + lineNum++ + ") ");
System.out.println(file.toLowerCase());
String line = br.readLine();
//String is split or removes the spaces and places into the array words
String[] words = line.split(" ");
//for loop to keep running on the length of the array
for(int i =0; i< words.length; i++){
//word is equal to a particular indexed spot of the word array and gets rid of all non-alphabet letters
String word = words[i];
word = word.replaceAll("[^a-z]", "");
}
}
}
catch (IOException e){
System.out.println("Error: " + e.getMessage());
}
}
}
另一類我是SLinkedList
將舉行的話到列表中,但就像我說的我無法弄清楚如何讓個別單詞到列表:
//Singly linked list
public class SLinkedList {
//head node of the list
protected Node head;
//number of nodes in the list
protected long size;
//Default constructor that creates an empty list
public SLinkedList(){
head = null;
size = 0;
}
}
我知道如何插入單鏈表的元素,而是試圖讓字到列表已經被證明是對我來說很難。任何事情都會對我很有幫助。
對不起,我忘了提,我需要輸入由句子的文件,我不能使用掃描儀 – user1267401 2012-03-13 23:48:32