【发布时间】:2013-02-02 08:19:41
【问题描述】:
您好,我想填写名为 QuestionIdList_Section 的 Arraylist 的 Arraylist
为此,我创建了名为 QUESTION_ID_Of_SectionId_Temp 的临时 Arraylist,添加到 QuestionIdList_Section 后会很清楚
我的代码如下,以便您了解我的编码方式:
public static ArrayList<String> QUESTION_ID_Of_SectionId_Temp = new ArrayList<String>();
public static ArrayList<ArrayList<String>> QuestionIdList_Section = new ArrayList<ArrayList<String>>();
QUESTION_ID_Of_SectionId_Temp.add("Hello");
QUESTION_ID_Of_SectionId_Temp.add("Hiii");
QuestionIdList_Section.add(0,QUESTION_ID_Of_SectionId_Temp);
Log.i(TAG, "******Before " + QuestionIdList_Section);
Log.i(TAG, "******Before "+ QUESTION_ID_Of_SectionId_Temp);
QUESTION_ID_Of_SectionId_Temp.clear();
Log.i(TAG, "******After " + QuestionIdList_Section);
Log.i(TAG, "******After " + QUESTION_ID_Of_SectionId_Temp);
执行代码后,我得到两个变量的不同结果。
如下:
******Before [[Hello, Hiiii]]
******Before [Hello, Hiiii]
******After [[]]
******After []
有人可以帮助我了解我在这里缺少的地方。我想清除临时数组列表,以便我可以为UESTION_ID_Of_SectionId_Temp 放置不同的值,因此对于QuestionIdList_Section 的第二个索引,我将设置不同的值。
提前致谢。
【问题讨论】:
-
您清除 QuestionIdList_Section 了吗?
-
使用
QUESTION_ID_Of_SectionId_Temp.remove(item)而不是QUESTION_ID_Of_SectionId_Temp.clear(); -
只需将
QuestionIdList_Section.add(0,QUESTION_ID_Of_SectionId_Temp);更改为QuestionIdList_Section.add(0,new ArrayList<String>(QUESTION_ID_Of_SectionId_Temp));,您的代码就可以工作了,因为您需要在清理之前复制 ArrayList 而不是传递引用 -
你好,作为一个提示,看看java的命名约定:oracle.com/technetwork/java/codeconv-138413.html