Java 访问两个循环之外的字符串数组

Java 访问两个循环之外的字符串数组,java,database,arrays,loops,derby,Java,Database,Arrays,Loops,Derby,我在for循环中嵌套了while循环中创建的字符串数组。这与在Derby中从字符串列创建数组有关,但为了简单起见,我将省略一些内容 我不提供完整代码的原因是因为问题非常具体。我的数据库、结果集、语句或查询没有任何问题。它只是访问两个循环之外的数组 //in the class, private int rowCount; //Count of the rows in my column of strings public String stringArray[]; //in the met

我在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.
}
//在类中,
私有整数行数//我的字符串列中的行数
公共字符串字符串数组[];
//在我使用的方法中
public void myMethod(){
rowCount=4;//在我的代码中,它实际上是对它进行计数的,假设它是4。
stringArray=new stringArray[rowCount];//设置
对于(int i=0;i

谢谢

嵌套循环有问题。对于每一行(i的每一个值/外部循环的每一次执行),您迭代整个结果集并多次覆盖
stringArray[i]
(我不更改)

当您到达第二行(即i为1或更高)时,rs.next()将已经为false,因为您在外部循环的第一次迭代中转换了整个rs


也许您只需要在实际代码中调用
rs.next()
来替换内部循环
while(rs.next())
,您可以说:

rowCount = 4; //In my code it actually counts it, lets say its four though.
您通过执行以下操作来执行行计数:

for( rowCount = 0; rs.next(); rowCount++ ) { }
如果您正在执行类似的操作,那么您基本上已经在计数阶段读取了所有行,当您稍后尝试重新处理这些行时,rs.next()方法将返回false,因为它已经读取了所有行


当然,我只是猜测…

那么问题是什么呢?您可以在循环之外访问数组。您的问题在别处,而不是在循环中打印字符串时读取valuestry(或者使用调试器查看字符串中实际放入的内容)。我打赌你的值是空的,你确定你的循环实际上填充了任何东西吗?例如,您真的想要
.getString(2)
?如何“打印值”?我实际上通过将循环更改为以下方式解决了这个问题:for(inti=0;I