Java 返回时Arraylist为空
我正在从事一个android项目,我面临一个问题,问题是: Arraylist在返回时为空 以下是我的java代码:Java 返回时Arraylist为空,java,android,collections,Java,Android,Collections,我正在从事一个android项目,我面临一个问题,问题是: Arraylist在返回时为空 以下是我的java代码: ArrayList<ArrayList<Object>> container = new ArrayList<ArrayList<Object>>(); ArrayList<Object> itemRow = new ArrayList<Object>();
ArrayList<ArrayList<Object>> container = new ArrayList<ArrayList<Object>>();
ArrayList<Object> itemRow = new ArrayList<Object>();
JSONObject jsonObj = new JSONObject(result);
JSONArray allElements = jsonObj.getJSONArray("Table");
Log.i("allElements", "" + allElements);
for (int i = 0; i < allElements.length(); i++) {
itemRow.add(allElements.getJSONObject(i).getString("ParentName").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentEmailID").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentContact").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentAddress").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentProfilePictureName").toString());
itemRow.add(allElements.getJSONObject(i).getString("StudentName").toString());
Log.i("itemRow", "itemRow at index: " + i + ", " + itemRow);
container.add(((i*2)/2), itemRow);
itemRow.clear();
}
return container;
我不明白为什么会发生这种情况,请帮我解决这个问题
谢谢。因为您这样做了,所以它仍然引用添加到
容器中的对象
itemRow.clear();
您可能想重新初始化它
itemRow = new ArrayList<Object>();
itemRow=newarraylist();
因为您这样做了,所以它仍然引用添加到容器中的对象
itemRow.clear();
您可能想重新初始化它
itemRow = new ArrayList<Object>();
itemRow=newarraylist();
停止清除列表,列表将不再为空:
itemRow.clear();
您应该在每次迭代中创建一个新列表。将以下代码行放入for循环中:
ArrayList<Object> itemRow = new ArrayList<Object>();
ArrayList itemRow=new ArrayList();
请记住,Java将引用传递给对象。因此,容器列表包含对您添加到其中的列表的引用。它不会复制列表。因此,当前代码将对同一列表对象的多个引用添加到容器列表中,并且每次添加列表时都会清除该列表。因此,它在循环结束时包含对同一空列表的N个引用。停止清除列表,它将不再为空:
itemRow.clear();
您应该在每次迭代中创建一个新列表。将以下代码行放入for循环中:
ArrayList<Object> itemRow = new ArrayList<Object>();
ArrayList itemRow=new ArrayList();
请记住,Java将引用传递给对象。因此,容器列表包含对您添加到其中的列表的引用。它不会复制列表。因此,当前代码将对同一列表对象的多个引用添加到容器列表中,并且每次添加列表时都会清除该列表。因此,它包含对循环末尾相同空列表的N个引用。您的评估有误导性/不正确,ArrayList不是空的,实际上包含五个元素
数组列表的每个元素都是空列表。这是因为循环中的最后两行:
container.add(((i*2)/2), itemRow);
itemRow.clear();
第一行将itemRow添加到容器中,正如您所期望的那样。下一行对刚刚添加的行调用clear()
,因此在方法退出时,容器中的所有内容都将始终为空
这个问题似乎是由于您试图在整个方法中重用相同的itemRow
对象而导致的,这是行不通的。要解决您的问题,请移动
ArrayList<Object> itemRow = new ArrayList<Object>();
ArrayList itemRow=new ArrayList();
构造函数(作为第一行),然后在最后停止对其调用clear()
。现在,每个JSON元素都将为其创建一个单独的行列表,一旦您将它们添加到容器中,它们将保留其内容。您的评估是误导性的/不正确的,ArrayList不是空的,实际上包含五个元素
数组列表的每个元素都是空列表。这是因为循环中的最后两行:
container.add(((i*2)/2), itemRow);
itemRow.clear();
第一行将itemRow添加到容器中,正如您所期望的那样。下一行对刚刚添加的行调用clear()
,因此在方法退出时,容器中的所有内容都将始终为空
这个问题似乎是由于您试图在整个方法中重用相同的itemRow
对象而导致的,这是行不通的。要解决您的问题,请移动
ArrayList<Object> itemRow = new ArrayList<Object>();
ArrayList itemRow=new ArrayList();
构造函数(作为第一行),然后在最后停止对其调用clear()
。现在,每个JSON元素都将为其创建一个单独的行列表,一旦您将它们添加到容器中,它们将维护其内容。您认为容器实际上复制了每个arraylist本身是不对的。它指的是那些已经创建的列表,而不是每个列表的副本。您认为容器实际上复制了每个arraylist本身是不正确的。它指的是那些已经创建的列表,而不是每个列表的副本。试试这个
container.add(((i*2)/2), itemRow.clone());
因为它是关于JAVA引用的 试试这个
container.add(((i*2)/2), itemRow.clone());
因为它是关于JAVA引用的