我正在做一个提取文件的多重排序,并按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 ++) {

关于java - ArrayList和Array multiSort,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40382984/

10-09 02:35