此赋值的目的是将文件中的单词列表输出到单链列表中,然后按字母顺序排序。然而,我不知道如何将单个单词放入链表中。我是这方面的新手,任何提示或暗示都将不胜感激。
这是我的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;
}
}
我知道如何插入单链表的元素,但事实证明,试图将单词插入列表对我来说很困难。任何事情都会对我很有帮助。
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
扫描仪还可以使用除空白之外的分隔符。这示例从字符串中读取几个项目:
String input = "1 fish 2 fish red fish blue fish"; Scanner s = new Scanner(input).useDelimiter("\s*fish\s*"); System.out.println(s.nextInt()); System.out.println(s.nextInt()); System.out.println(s.next()); System.out.println(s.next()); s.close();
循环直到文件结束,缓存当前单词,然后将其插入当前节点中。然后移动到链接中的下一个节点,然后重复。
完成后,您将不得不对节点进行某种排序和交换。
试试这个,希望对你有用。。。
import java.util.*;
import java.io.*;
public class FileLinkList
{
public static void main(String args[])throws IOException{
String content = new String();
int count=1;
File file = new File("abc.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()){
content = sc.nextLine();
list.add(content);
}
sc.close();
}catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("nProgram terminated Safely...");
}
Collections.reverse(list);
Iterator i = list.iterator();
while (i.hasNext()) {
System.out.print("Node " + (count++) + " : ");
System.out.println(i.next());
}
}
}