我有一個項目在我的comp 182班,我們正在處理矢量,但我被困在製作一個「有序矢量」。當我嘗試運行它時,我得到一個ArrayOutofBounds錯誤。
(其中「的howmany」變量是使用該「addWord爲字符串在陣列「theWords」
和代碼是從另一個類中讀出與在其10個字的輸入文件運行大小的計數, 「方法來添加從該文件的話進入‘theWords’陣列)
下面的代碼我到目前爲止:
[順便說一句我們不允許使用‘陣列’的方法只有‘的compareTo’]Java - ordered/alphabetizing字符串數組
public void addWord(String newWord) {
//adds in words
if (howMany < theWords.length) {
theWords[howMany]= newWord;
howMany++;
}
else {
String t [] = new String[capacity+10];
for (int i=0; i <capacity; i++){
t[i] = theWords[i];
}
theWords = t;
theWords[howMany] = newWord;
howMany++;
}
//ordering words
for(int g = howMany - 1, z = howMany ; g < howMany; g--, z--) {
if(newWord.compareTo(theWords[g]) < 0) {
theWords[g] = theWords[z];
theWords[g] = newWord;
}
else
newWord = theWords[z];
}
howMany++;
}
任何幫助非常感謝!
哪一行,你得到的異常並請張貼staccktrace – PermGenError