public String[][] getArray(ArrayList rs){
rs = this.doExcel();
String[][] excel_str=new String[rs.size()][]; //转换为二维数组
try{
for(int i=0;i<excel_str.length;i++){
ArrayList temp=(ArrayList)rs.get(i);
excel_str[i]=(String[])(temp.toArray(new String[0]));
}
}catch(Exception e){
System.err.println(e.getMessage());
}
return excel_str;
}
这是我写的一个方法,运行时可以得到ArrayList,是正确的结果,但是在执行到excel_str[i]=(String[])(temp.toArray(new String[0]));这行语句时,就有问题,catch到null,不知道为什么会这样,那位能指点一二,谢谢
rs = this.doExcel();
String[][] excel_str=new String[rs.size()][]; //转换为二维数组
try{
for(int i=0;i<excel_str.length;i++){
ArrayList temp=(ArrayList)rs.get(i);
excel_str[i]=(String[])(temp.toArray(new String[0]));
}
}catch(Exception e){
System.err.println(e.getMessage());
}
return excel_str;
}
这是我写的一个方法,运行时可以得到ArrayList,是正确的结果,但是在执行到excel_str[i]=(String[])(temp.toArray(new String[0]));这行语句时,就有问题,catch到null,不知道为什么会这样,那位能指点一二,谢谢
这么用是不行的,会出现错误提示:type mismatch:cannot convert from Object[] to String[]
rs = this.doExcel(); rs是参数啊,他的值还没有使用就被覆盖了,要这个参数有啥意义啊?
不知道有没有为空的情况!
要不就用这个
excel_str[i]=(String[])(temp.toArray());
excel_str[i]=temp.toArray(new String[0]);这个用法是没有错误的,先前我已经用过,没有问题,只不过现在我要操作excel文件,将数据存储到一个ArrayList后,转换成数组就有问题,不知和解,但如果是将一个结果集存入ArrayList后,用同样的方法是没有问题的
String[] temp = (String[])rs.toArray(new String[rs.size()]);自己在变一变吧
第二, excel_str[i]=(String[])(temp.toArray(new String[0]));String[]是数组的声明方法,不能这样的转换,应该老老实实的
String[] array;
for(int j = 0 ; j < temp.size();j++)
{
array[j] = (String)temp.get(j);
}
这样array就是你要的数据了
第三,如果你想用toArray()方法,那应该声明一个Object[]数组,然后逐一的转换成String类型;(这个你应该会,我就不写了)
第四,如果你想用toArray(Object[] a)方法,应该这样:
Object[] array;
excel_str[i]=temp.toArray(array);
这样array就是你要的数据,并且重复第三步
ArrayList temp=(ArrayList)rs.get(i); // i --> 0 ~ excel_str.length
System.out.println("rs.get("+i")=" + temp);
没有null的,是正确的结果
------String[][] excel_str=new String[rs.size()][10000]; //转换为二维数组