【发布时间】:2016-02-28 20:02:45
【问题描述】:
这个赋值的目的是将文件中的单词列表输出到单链表中,然后按字母顺序对它们进行排序。但是我无法弄清楚如何将单个单词放入链接列表中。我对此很陌生,任何提示或提示将不胜感激。
这是我认为是正确的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;
}
}
我知道如何在单链表中插入元素,但是尝试将单词放入列表中对我来说很困难。任何事情都会对我很有帮助。
【问题讨论】:
标签: java linked-list text-files