我使用 ResultSet 从 mysql 表中为每一行获取 3 列。我不确定使用查询将获得多少行。为了存储这些行,我使用 ArrayList
作为下面给定的代码:
while (rs.next())
{
String[] row = new String[numcols];
for (int i = 0; i < numcols; i++)
{
row[i] = rs.getString(i + 1);
}
rowsList.add(row);
}
rs.close();
当我调试代码时,我发现列值存在于 ArrayList
中。现在我无法访问列值,因为 rowsList.get(index)
将仅返回特定索引处的值,但我在该索引处还有另外 3 个值如何访问这些值。
最佳答案
List.get()
在您的例子中将返回一个 String
数组:String[]
。您可以使用 []
索引运算符访问数组的元素:
String[] row = rowList.get(0);
for (int i = 0; i < row.length; i++)
System.out.println(row[i]);
// Or you can use the enhanced for to loop through the array:
for (String s : row)
System.out.println(s);
如果要处理所有行:
for (String[] row : rowList) {
// Process row
for (String s : row) {
// Do something with the string
}
}
关于java - 访问多维 ArrayList 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25616307/