我有一個嵌套在for循環內的while循環內創建的字符串數組。這與從Derby中的字符串列創建數組有關,但爲了簡單起見,我將省略一些東西。訪問字符串兩個循環外的數組
我不提供完整代碼的原因是因爲問題非常具體。我對我的數據庫,結果集,語句或查詢沒有任何問題。它只是訪問兩個循環之外的數組。
//in the class,
private int rowCount; //Count of the rows in my column of strings
public String stringArray[];
//in the method I am using
public void myMethod() {
rowCount = 4; //In my code it actually counts it, lets say its four though.
stringArray = new stringArray[rowCount]; //set
for (int i = 0; i < rowCount; i++) {
while (rs.next()/*rs is simply a result set of a statement executeQuery to select the correct table*/)
{
stringArray[i] = rs.getString(2); //2 is the column number in the table
}
}
//Need to access stringArray here. When I try to print out values, it always prints out as null.
}
謝謝!
那麼,什麼是問題? – gma
你可以訪問循環外的數組。你的問題是在其他地方,而不是讀取值 –
嘗試在你的循環內打印字符串**(或者使用調試器來查看實際放入字符串的內容)。我敢打賭,你的價值觀也是空的 –