我正在做一个多重排序,提取一个文件并按1)元音计数和2)单词长度进行排序。我试着搜索,我昨天发布的比我应该更困惑。我已经把昨天的问题整理好了,只剩下一件没做完。
我几乎完成了第一遍的整个开始,直到我得到清理和打印部分。
我包含了整个代码,因为我不确定问题可能在哪里,我也包含了我的堆栈,因为有相当数量的错误。
这一节似乎工作正常
public static void main(String[] args) throws FileNotFoundException {
String[] theList = readStoreFile();
displayArray("Word in the file ...", theList);
firstPass(theList);
}
private static String[] readStoreFile() throws FileNotFoundException {
File file = getFile();
String[] initialList = writeFile(file);
return initialList;
}
private static File getFile() {
String homedir = System.getProperty("user.home");
System.out.print("homedir = " + homedir);
JFileChooser jfc = new JFileChooser(new File(homedir));
jfc.showOpenDialog(null);
File file = jfc.getSelectedFile();
return file;
}
private static String[] writeFile(File file) throws FileNotFoundException {
Scanner scanner = new Scanner(file);
System.out.println("File Name ... " + file);
ArrayList<String> initialList = new ArrayList();
while (scanner.hasNext()) {
String word = scanner.next().toLowerCase();
initialList.add(word);
}
return changeToArray(initialList);
}
问题开始
private static String[] firstPass(String[] initialList) {
ArrayList<LinkedList<String>> vowelQueues = getQueues(highestVowelCount(initialList) + 1);
for (String word : initialList) {
vowelQueues.get(numberOfVowels(word)).add(word);
}
displayQueues("Sorted by vowel count ... n" , vowelQueues);
return emptyQueues(vowelQueues);
}
private static int highestVowelCount(String[] initialList) {
int highCount = 0;
for (String word : initialList) {
int vowelCount = numberOfVowels(word);
if (vowelCount > highCount){
highCount = vowelCount;
}
}
return highCount;
private static int numberOfVowels(String word) {
int vowelCount = 0;
for (char c : word.toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
vowelCount++;
}
return vowelCount;
}
我在添加时得到一个错误,这是由其他人协助的,我试图将队列总数添加到最高元音计数+ 1的数量。
private static ArrayList<LinkedList<String>> getQueues(int i) {
ArrayList<LinkedList<String>> queues = new ArrayList(i);
for (int count = 0; count < i; i ++) {
queues.add(new LinkedList());
}
return queues;
}
private static void displayArray(String intro, String[] array) {
System.out.println(intro);
int count =0;
for (String printWord : array){
System.out.print(printWord + " ");
count ++;
if (count == 11) {
System.out.println();
}
}
}
private static String[] changeToArray(ArrayList<String> initialList) {
String[] array = new String[initialList.size()];
for (int count = 0; count < array.length; count ++) {
array[count] = initialList.get(count);
}
return array;
}
private static void displayQueues(String intro, ArrayList<LinkedList<String>> queues) {
System.out.println(intro);
for (int count = 0; count < queues.size(); count++) {
System.out.print("Queue " + count + queues.get(count).toString() + "n");
}
}
private static String[] emptyQueues(ArrayList<LinkedList<String>> queues) {
ArrayList<String> delete = new ArrayList();
for (LinkedList<String> queue : queues){
while (!queue.isEmpty())
delete.add(queues.remove());
}
return changeToArray(delete);
}
}
这是我的堆栈
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:261)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at textprocessing.MultiSortV2.getQueues(MultiSortV2.java:88)
at textprocessing.MultiSortV2.firstPass(MultiSortV2.java:57)
at textprocessing.MultiSortV2.main(MultiSortV2.java:26)
/Users/jackschirtz/Library/Caches/NetBeans/8.1/executor-snippets/debug.xml:83: Java returned: 1
BUILD FAILED (total time: 49 seconds)
在getQueues(int i)
中存在一个无限循环。
for
中的第三个参数应该是count ++
而不是i ++
。
试
for (int count = 0; count < i; count ++) {
不是
for (int count = 0; count < i; i ++) {