Java 数组列表和数组
我在一个数组中有2个ArrayList,如下所示:Java 数组列表和数组,java,arrays,arraylist,Java,Arrays,Arraylist,我在一个数组中有2个ArrayList,如下所示: public Object[] loopthrougharrays() { Object[] tables = new Object[2]; tables[0] = list; tables[1] = listB15; return tables; } 我的两个数组列表分别称为list和listB15 然后我可以从另一个方法调用ArrayList,如 loopthrougharrays()[1] = new A
public Object[] loopthrougharrays() {
Object[] tables = new Object[2];
tables[0] = list;
tables[1] = listB15;
return tables;
}
我的两个数组列表分别称为list
和listB15
然后我可以从另一个方法调用ArrayList,如
loopthrougharrays()[1] = new ArrayList();
这是列表B15
但是,如果我尝试向ArrayList
添加一个项目
loopthrougharrays()[1].add(s)
其中,s
是一个变量
Java无法将loopthroughrays()[1]
识别为ArrayList
如何通过此方法添加变量
我似乎遇到以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
如果我执行以下操作,我的整个代码工作正常:
listB15 = new ArrayList();
listB15.add(s)
如我所料。在您向列表中添加任何内容之前,请先将其转换
((ArrayList<YourVariableType>)(loopthrougharrays()[1])).add(s);
((ArrayList)(loopthroughrays()[1])。添加;
但请记住,这不是一个好的做法。我敢肯定,通过更好的设计和最佳实践,您可以实现您想要实现的目标。在您向列表中添加任何内容之前,先将其转换
((ArrayList<YourVariableType>)(loopthrougharrays()[1])).add(s);
((ArrayList)(loopthroughrays()[1])。添加;
但请记住,这不是一个好的做法。我很确定,通过更好的设计和最佳实践,您可以实现您想要实现的目标。我不知道您想用它做什么,但这不是一个好的实践。但是,如果将对象数组元素强制转换为ArrayList(如:
((ArrayList)t.loopthrougharrays()[1]).add(s);
并删除loopthroughrays()[1]=newarraylist()代码>(如@Tom comment says,谢谢)或将其替换为
ArrayList myNewArrayList = (ArrayList)loopthrougharrays()[1];
myNewArrayList.add(s);
我不知道你想怎么做,但这不是一个好的做法。但是,如果将对象数组元素强制转换为ArrayList(如:
((ArrayList)t.loopthrougharrays()[1]).add(s);
并删除loopthroughrays()[1]=newarraylist()代码>(如@Tom comment says,谢谢)或将其替换为
ArrayList myNewArrayList = (ArrayList)loopthrougharrays()[1];
myNewArrayList.add(s);
您的表是一个对象数组
表[1]=listB15
//表仍然是对象的数组
,而listB15被视为对象,因为对象是超类
要实现wt u want,您可以尝试以下方法:
((列表)表[1])。添加(“hello world”);//使用列表(例如)强制转换它
创建一个列表
List<List<String>> tables = new ArrayList<List<String>>();
tables1.add(list) ;
tables1.add(listB15) ;
tables1.get(1).add("hello world");
System.out.println(tables);
List tables=new ArrayList();
表1.添加(列表);
表1.add(列表B15);
表1.get(1).add(“helloworld”);
系统输出打印LN(表格);
您的表是一个对象数组
表[1]=listB15
//表仍然是对象的数组
,而listB15被视为对象,因为对象是超类
要实现wt u want,您可以尝试以下方法:
((列表)表[1])。添加(“hello world”);//使用列表(例如)强制转换它
创建一个列表
List<List<String>> tables = new ArrayList<List<String>>();
tables1.add(list) ;
tables1.add(listB15) ;
tables1.get(1).add("hello world");
System.out.println(tables);
List tables=new ArrayList();
表1.添加(列表);
表1.add(列表B15);
表1.get(1).add(“helloworld”);
系统输出打印LN(表格);
问题是您正在对方法调用执行赋值。
您正在调用该方法两次。
因此第二行创建了另一个数组列表
loopthrougharrays()[1] = new ArrayList();
loopthrougharrays()[1].add(s) //This one will call the method again
// and get new array list and the previous value is lost
简单修复
Object[] getTwoArrays = loopthrougharrays()
ArrayList L0 = <ArrayList> getTwoArrays[0];
ArrayList L1 = <ArrayList> getTwoArrays[1];
L1 = new ArrayList();
L1.add(s);
问题是您正在对方法调用执行赋值。
您正在调用该方法两次。
因此第二行创建了另一个数组列表
loopthrougharrays()[1] = new ArrayList();
loopthrougharrays()[1].add(s) //This one will call the method again
// and get new array list and the previous value is lost
简单修复
Object[] getTwoArrays = loopthrougharrays()
ArrayList L0 = <ArrayList> getTwoArrays[0];
ArrayList L1 = <ArrayList> getTwoArrays[1];
L1 = new ArrayList();
L1.add(s);
如果有一个对象数组
,编译器如何知道它包含哪些实例?顺便说一句:之所以得到NPE,是因为loopthroughrays()
创建了一个新数组,而这与loopthroughrays()[1]=newarraylist()无关代码>statement.tl;dr:将表
制作成数组列表[]
(或者更好的是制作成列表[]
)。如果有一个对象的数组
,编译器应该如何知道它包含哪些实例?顺便说一句:之所以得到NPE,是因为loopthroughrays()
创建了一个新数组,而这与loopthroughrays()[1]=newarraylist()无关代码>statement.tl;dr:将表格
制作成数组列表[]
(或者更好地制作成列表[]
)。