2014-02-15 22 views
1

對於我的項目,用戶必須定義​​一個數組的大小,並且我創建了一個集合方法來做到這一點。被更改的變量是類變量,arraySize。但是,當有人設置數組大小,並嘗試添加數組時,我收到一個indexoutofbounds錯誤。當在類中定義數組大小時,爲什麼會得到ArrayIndexOutOfBounds異常?

這裏的代碼段的

//Size of the array 
private int arraySize; 
//initializes elemnt count of array 
private int arrayElementCount; 

//Gets count of SID in array 
public int getSIDArrayCount() { 

    //returns private variable 
    return arrayElementCount; 
} 

//Sets SID Array 
public void setArraySize(int setArraySize) { 
    //Array size int 
    //checks array if array size is greater than 0 
    if (setArraySize > 0) { 
     //sets array size 
      //SIDArray 
     arraySize = setArraySize; 
    } 

} 

//SIDArray 
private String[] SIDArray = new String[arraySize]; 

編輯: ,因爲我需要爲陣列的訪問方法,我不能初始化在「setArraySize」法陣。

這裏是班上的其他同學:

/* 

*要改變這種許可證頭,選擇在項目屬性許可頭。 *要更改此模板文件,請選擇工具|模板 *並在編輯器中打開模板。 */ package com.ahellhound.pkg202sidproject;

進口java.util.Arrays中; import java.util.regex.Pattern;

/** * * @author科爾比勒克萊爾 */ 公共類SIDArrayTest {

//Size of the array 
private int arraySize; 
//initializes elemnt count of array 
private int arrayElementCount; 

//Gets count of SID in array 
public int getSIDArrayCount() { 

    //returns private variable 
    return arrayElementCount; 
} 

//Sets SID Array 
public void setArraySize(int setArraySize) { 
    //Array size int 
    //checks array if array size is greater than 0 
    if (setArraySize > 0) { 
     //sets array size 
      //SIDArray 
     arraySize = setArraySize; 
    } 

} 

//SIDArray 
private String[] SIDArray = new String[arraySize]; 
//Gets SID Array 
public String[] getSIDArray() { 

    //returns array 
    return SIDArray; 
} 

//Validates SID; returns true if SID entry is valid 
public boolean validateSID(String SIDEntry) { 
    //checks if sid entry is 7, and if starts with s 
    if (SIDEntry.length() != 7) { 
     //retuns false 
     return false; 

    } 

    //checks if SIDEntry begins with 'S' 
    if (!SIDEntry.matches("[sS]\\d{6}")) { 

     //returns false 
     return false; 
    } 
    //returns true 
    return true; 
} 

//Adds SID to the Array 
public void addSID(String SIDEntry) { 

    //checks if SID is valid 
    if (validateSID(SIDEntry)) { 
     //Adds sid to array 
     SIDArray[arrayElementCount] = SIDEntry; 
     //increases array size by one 
     arrayElementCount++; 

    } else { 

     System.out.println("test failed 2"); 
    } 

} 

//Gets SID array and returns all entrys to strings 
public String getSIDArrayString() { 
    //Gets array and converts to string 
    String SIDArrayString = Arrays.toString(SIDArray); 
    //returns array string 
    return SIDArrayString; 

} 

}

我如何設置數組的大小,當我真正難倒我們爲它創建了一個類變量,同時讓這個數組成爲一個'get'-able方法。

回答

0

您實例呼叫您setArraySize-方法之前陣列,從而它得到的0.

//SIDArray 
private String[] SIDArray = new String[arraySize]; 

由於SIDArray變量是在類的範圍大小,它被儘快實例化爲對象的類被創建。因此,它使用arraySize varible,在這一點上沒有實例化(如果是int,默認值爲0)。

要解決這個問題,只需在調用setArraySize()後實例化數組。

下面是一些代碼,以澄清我在評論中的含義。

public class A { 
    /* In your code, you instantiate the array at this point, in the scope of the class, while arraySize is still uninitialized */ 
    String[] array; /* uninitialized, defaults to null */ 
    int arraySize; /* uninitialized, defaults to 0 */ 

    /* Simple accessor method for the array */ 
    public String[] getArray() { 
     return array; 
    } 

    public int getArraySize() { 
     return arraySize; 
    } 


    public void setArraySize(int size) { 
     this.arraySize = size; 
    } 

    public void createArray() { 
     array = new String[arraySize]; 
    } 
} 
+0

這個問題是,我需要一個數組的訪問器方法,如果該數組在方法中初始化,我不能擁有該方法。 – Colby

+0

你可以有一個數組的訪問方法,無論它在哪裏初始化 – Zavior

+0

訪問方法就像這樣簡單:public String [] getArray(){return yourArray; } – Zavior

0

您必須在設置新大小後初始化數組變量。你的方法更改爲:

//Sets SID Array 
public void setArraySize(int setArraySize) { 
    //Array size int 
    //checks array if array size is greater than 0 
    if (setArraySize > 0) { 
     //sets array size 
     //SIDArray 
     arraySize = setArraySize; 
     SIDArray = new String[arraySize]; 
    } 
} 
+0

感謝您的回覆,但我需要一個數組的訪問器方法。 – Colby

+0

您已經擁有數組的存取方法!按照我的建議更改setArraySize方法實現。 – RaviH

0

的問題是,你是用實例化一個固定大小的數組(0)。這不一定是壞的......真正的問題是,在你的方法setArraySize(int setArraySize),你更新你的arraySize變量,但數組永遠不知道這個調用! 你應該使用類似

SIDArray = new String[arraySize]; 

但是,即使你做到這一點,調整您的陣列會破壞它的所有以前的內容...只是意識到這一點。

相關問題