List和二维数组之间转化及初始化
ArrayList resultsList = new ArrayList();
String[] result = { "cr_tx_amt",(f_fare", "counts" };
resultsList.add(result);
String[][] results = new String[resultsList.size()][];
for (int i = 0; i < resultsList.size(); i++) {
String[] temp = (String[]) resultsList.get(i);
results[i] =new String[temp.length];
System.out.print(temp.length);
for (int j = 0; j < temp.length; j++) {
results[i][j] = temp[j];
}
}
return results;
注意:无论是一维数组还是二维,每一维都必须指定长度,要不然会默认只有一个元素的空间。最好不要初始化为null。
定义一个类型的二维数组 String[][] a;
定义一维数组长度 a = new String[i][];
定义二维数组长度 a[i] = new String[j]
例子:将一个字符串的内容分隔,并且放入一个二维数组中
public class TestToString {
public static void main(String[] args) {
String s = "0,1;3,6,4;7,1";
String[] a = s.split(";");
double[][] d;
d = new double[a.length][];
for(int i=0; i<a.length; i++){
String[] s_num = a[i].split(",");
for(int j=0; j<s_num.length; j++){
d[i] = new double[s_num.length];
d[i][j] = Double.parseDouble(s_num[j]);
System.out.println("d[" + i + "][" + j +"] = " + d[i][j]);
}
}
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
