0
我正在做一個multisort,它將拉取一個文件並按1)元音計數和2)字長進行排序。我試圖搜索,我昨天發佈比我應該更困惑。我從昨天開始整理了我的問題,除了我有一個我無法解決的問題。ArrayList和Array multiSort
我幾乎是通過第一遍的整個開始,直到我到達清晰和打印部分。
Ive包括了整個代碼,因爲我不確定問題出在哪裏,我還包括了我的堆棧,因爲有很多錯誤。
本節看似WORKS問題
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;
}
我得到的附加誤差的精細
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);
}
開始,這是由別人幫助時,我嘗試添加量隊列總量達到最高元音量計算+ 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)
您是否嘗試過與小大小的文件?你的Java應用程序出現內存不足 – Badr
該文件是「你生日快樂」我不知道爲什麼它有問題。它和我的瀏覽器是唯一正在運行的東西。 –
而我仍然有錯誤,不管我不?我的意思是在代碼中突出顯示了部分內容,我無法通過netbeans錯誤消息找出 –