I have to create a dictionary where you input a text file of 5 sentences and it takes the words in them and sorts them alphabetically using a singly linked list. I have the text file but really need help with making them into a linked list and sorting them. I understand how to create linked lists but I don't know how to create them from a text file and sort them. Any help would be appreciated.
我必须创建一个字典,您输入一个包含5个句子的文本文件,并在其中输入单词并使用单个链接列表按字母顺序对它们进行排序。我有文本文件,但真的需要帮助将它们变成链表并对它们进行排序。我理解如何创建链接列表,但我不知道如何从文本文件创建它们并对它们进行排序。任何帮助,将不胜感激。
import java.util.*;
public class Dictionary {
public static void main(String[] args) {
String[] things = {"a", "dog", "eats"};
List<String> list1 = new LinkedList<String>();
for(String x : things)
list1.add(x);
String[] things2 = {"The", "Cat", "Walks"};
List<String> list2 = new LinkedList<String>();
for(String y : things2)
list2.add(y);
list1.addAll(list2);
list2 = null;
printMe(list1);
printMe(list1);
}
private static void printMe(List<String> l) {
for(String b : l)
System.out.printf("%s ", b);
System.out.println();
}
}
2 个解决方案
#2
0
Well, the Scanner class has methods to iterate through tokens based on a pattern you supply it. You can supply the pattern (a regular expression) either on each call to "hasNext(Pattern)" and "next(Pattern)" or by calling the "usePattern(Pattern)" method to set the default pattern, and using the standard "hasNext()" and "next()" iterator methods.
嗯,Scanner类有基于你提供的模式迭代令牌的方法。您可以在每次调用“hasNext(Pattern)”和“next(Pattern)”时提供模式(正则表达式),或者通过调用“usePattern(Pattern)”方法来设置默认模式,并使用标准“ hasNext()“和”next()“迭代器方法。
If you don't set any pattern it uses this:
如果你没有设置任何模式,它使用它:
// A pattern for java whitespace
private static Pattern WHITESPACE_PATTERN = Pattern.compile(
"\\p{javaWhitespace}+");
I won't get into the regular expressions here, but your general flow would be:
我不会在这里介绍正则表达式,但是你的一般流程将是:
Scanner scanner = new Scanner(reader);
scanner.usePattern(Pattern.compile("some regex pattern")); // if you want something other than the default
while (scanner.hasNext()) {
String word = scanner.next();
}
Likely reader would be an instance of java.io.FileReader. And you want a better throughput for a large file, wrap the FileReader in a java.io.BufferedReader.
可能的读者将是java.io.FileReader的一个实例。并且您希望为大文件提供更好的吞吐量,将FileReader包装在java.io.BufferedReader中。
For sorting, you can either sort with Collections.sort() after adding all the words, or while adding each word, you can iterate through the existing linked list with the ListIterator returned by List.listIterator() method, find the first element which is lexigraphically greater than that token, and insert before that token using the ListIterator.add() method.
对于排序,您可以在添加所有单词后使用Collections.sort()进行排序,或者在添加每个单词时,您可以使用List.listIterator()方法返回的ListIterator迭代现有链表,找到第一个元素在词典上大于该标记,并使用ListIterator.add()方法在该标记之前插入。
#1
#2
0
Well, the Scanner class has methods to iterate through tokens based on a pattern you supply it. You can supply the pattern (a regular expression) either on each call to "hasNext(Pattern)" and "next(Pattern)" or by calling the "usePattern(Pattern)" method to set the default pattern, and using the standard "hasNext()" and "next()" iterator methods.
嗯,Scanner类有基于你提供的模式迭代令牌的方法。您可以在每次调用“hasNext(Pattern)”和“next(Pattern)”时提供模式(正则表达式),或者通过调用“usePattern(Pattern)”方法来设置默认模式,并使用标准“ hasNext()“和”next()“迭代器方法。
If you don't set any pattern it uses this:
如果你没有设置任何模式,它使用它:
// A pattern for java whitespace
private static Pattern WHITESPACE_PATTERN = Pattern.compile(
"\\p{javaWhitespace}+");
I won't get into the regular expressions here, but your general flow would be:
我不会在这里介绍正则表达式,但是你的一般流程将是:
Scanner scanner = new Scanner(reader);
scanner.usePattern(Pattern.compile("some regex pattern")); // if you want something other than the default
while (scanner.hasNext()) {
String word = scanner.next();
}
Likely reader would be an instance of java.io.FileReader. And you want a better throughput for a large file, wrap the FileReader in a java.io.BufferedReader.
可能的读者将是java.io.FileReader的一个实例。并且您希望为大文件提供更好的吞吐量,将FileReader包装在java.io.BufferedReader中。
For sorting, you can either sort with Collections.sort() after adding all the words, or while adding each word, you can iterate through the existing linked list with the ListIterator returned by List.listIterator() method, find the first element which is lexigraphically greater than that token, and insert before that token using the ListIterator.add() method.
对于排序,您可以在添加所有单词后使用Collections.sort()进行排序,或者在添加每个单词时,您可以使用List.listIterator()方法返回的ListIterator迭代现有链表,找到第一个元素在词典上大于该标记,并使用ListIterator.add()方法在该标记之前插入。