在java中将arraylist传输到双数组[0]
我有以下代码:在java中将arraylist传输到双数组[0],java,multidimensional-array,Java,Multidimensional Array,我有以下代码: public class Test{ arrayList<String> list = new ArrayList<String>(); String[][] temp_list; public static void main(String[] args) { String temp = list.get(0); temp_list[0] = tem
public class Test{
arrayList<String> list = new ArrayList<String>();
String[][] temp_list;
public static void main(String[] args)
{
String temp = list.get(0);
temp_list[0] = temp.split(" ");
}
}
有人能帮我吗?在使用临时列表之前,您需要初始化临时列表。您需要指定数组的大小。例如:
int sizeOfArray = 5;
String[][] temp_list = new String[sizeOfArray][];
这是因为您尚未为临时列表分配任何2D数组。拆分结果应存储在哪个数组中 这是您的代码片段的工作版本
import java.util.ArrayList;
public class Test {
static ArrayList<String> list = new ArrayList<String>();
static String[][] temp_list;
public static void main(String[] args) {
list.add("hello wold");
// allocate memory for 10 string-arrays.
temp_list = new String[10][]; <-----------
String temp = list.get(0);
temp_list[0] = temp.split(" ");
}
}
此代码将不会编译,因为list声明为类的成员变量,但main是一个静态方法 如前所述,list也没有添加任何内容,因此对list.get0的调用将抛出异常NOTNULL指针
数组temp_列表在给定的代码中没有分配任何新的,因此尝试分配到该列表将引发空指针异常。指定的索引似乎。。向后
import java.util.ArrayList;
public class Test {
static ArrayList<String> list = new ArrayList<String>();
static String[][] temp_list;
public static void main(String[] args) {
list.add("hello wold");
// allocate memory for 10 string-arrays.
temp_list = new String[10][]; <-----------
String temp = list.get(0);
temp_list[0] = temp.split(" ");
}
}