在java项目中有异常
//Method to Convert the resultset into array[][].
public String [][] toArray(ResultSet e){
ArrayList<String> result = new ArrayList<String>(8);
String[][] finalResult;
try {
while (e.next()) {
result.add(e.getString(1));
result.add(e.getString(2));
result.add(String.valueOf(e.getFloat(3)));
result.add(e.getString(4));
result.add(e.getString(5));
result.add(e.getString(6));
}
}catch (Exception ex){
}
finalResult = new String[result.size()+1/6][6];
int j=0;
for (int i =0; i < result.size(); i+=6, j++){
finalResult[j][0] = result.get(i);
finalResult[j][1] = result.get(i+1);
finalResult[j][2] = result.get(i+2);
finalResult[j][3] = result.get(i+3);
finalResult[j][4] = result.get(i+4);
finalResult[j][5] = result.get(i+5);
}
return finalResult;
}
// Method to convert ArrayLit to 2D Array.
public String [][] toArray(ArrayList<String> e){
ArrayList<String> result = e;
String[][] finalResult;
finalResult = new String[result.size()+1/6][6];
int j=0;
for (int i =0; i < result.size(); i+=6, j++){
finalResult[j][0] = result.get(i);
finalResult[j][1] = result.get(i+1);
finalResult[j][2] = result.get(i+2);
finalResult[j][3] = result.get(i+3);
finalResult[j][4] = result.get(i+4);
finalResult[j][5] = result.get(i+5);
}
return finalResult;
}
exceptio:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
发布于 2018-08-27 14:00:49
尝试将其更改为:
finalResult = new String[(result.size()+1)/6][6];
可以尝试这个简单的测试来看看我的意思:
System.out.println(11 + 1/6); //Prints 11 (11 + 0)
System.out.println((11 + 1)/6); //Prints 2 (12/6)
https://stackoverflow.com/questions/-100002417
复制相似问题