2012-09-27 44 views
0

我有一個項目在我的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++; 
    } 

任何幫助非常感謝!

+1

哪一行,你得到的異常並請張貼staccktrace – PermGenError

回答

2

取決於陣列的容量,則該語句

theWords[g] = theWords[z] 

將在循環的第一次執行失敗,爲z =長度的數組(數組中最後一個索引是長度 - 1)。在一個側面說明,在循環中,您最初設置

g = howMany - 1 

,然後再遞減,所以摹<的howmany將永遠是真實的......你將有一個無限循環。當g或z低於0時,此無限循環也可能導致索引超出範圍異常。

1

如果在執行函數前執行howMany == capacity - 1,則會出現問題。

會發生什麼:你進入,如果(在//添加的話),那麼你增加howMany的第一個分支,它成爲howMany = capacity,然後分配z = howMany其邊界以外訪問數組:theWords[g] = theWords[z];

另一件事是你不增加capacity變量,它必須是capacity+=10

這裏是我的變種:

import java.util.Arrays; 
import java.util.Random; 
public class OrderedArray{ 

    int howMany = 0; 
    String[] theWords = new String[10]; 

    public void addWord(String newWord) { 

    //don't accept nulls 
    if(newWord == null) { 
     return; 
    } 
    //if length is reached increase the array 
    if(howMany >= theWords.length - 1) { 
     theWords = Arrays.copyOf(theWords, theWords.length+10); 
    } 

    //go through existing words and add newWord if it is less then met value 
    boolean isAdded = false; 
    for(int idx = 0; idx < howMany && theWords[idx] != null; idx++){               
     if(newWord.compareTo(theWords[idx]) < 0){ 
     isAdded = true; 
     String valToShift = theWords[idx]; 
     theWords[idx] = newWord; 

     //copy all values after the met index 
     for(int shIdx = idx+1; shIdx <= howMany && shIdx < theWords.length; shIdx++){ 
      String tmp = theWords[shIdx]; 
      theWords[shIdx] = valToShift; 
      valToShift = tmp; 
     }   
     break; 
     } 
    } 

    //if a value was not added then add it 
    if(!isAdded){ 
     theWords[howMany] = newWord; 
    } 
    ++howMany; 
    } 

    public String toString(){ 
    StringBuffer sb = new StringBuffer("howMany:"); 
    sb.append(howMany); 
    for(int idx = 0; idx < howMany; idx++){ 
     sb.append(","); 
     sb.append(theWords[idx]); 
    } 
    return sb.toString(); 
    } 


    public static void main(String[] args){ 
    OrderedArray m = new OrderedArray(); 
    Random r = new Random(System.currentTimeMillis()); 
    for(int i = 0; i < 210; i++){ 
     m.addWord(Math.abs(r.nextInt())+ ""); 
    } 
    System.out.println(m); 

    OrderedArray m1 = new OrderedArray(); 

    m1.addWord("c"); 
    m1.addWord("d"); 
    m1.addWord("a"); 
    m1.addWord("cd"); 

    System.out.println(m1); 

    } 

}