この割り当ての目的は、ファイルから単一リンクリストに単語のリストを出力し、アルファベット順に並べ替えることです。しかし、私はリンクされたリストに個々の単語を取得する方法を把握することはできません。私はこれで非常に新しいと任意のヒントやヒントは非常に感謝される。ここでテキストファイルから単一リンクリストへの単語の取得方法
は私が正しいと信じて私の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