Hello I want to Fill Up my Arraylist of Arraylist named as QuestionIdList_Section
and for that i have make the temp Arraylist named QUESTION_ID_Of_SectionId_Temp that will be clear after adding into QuestionIdList_Section
My Code is as Below so that you can understand how i have codded :
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);
After Executing the Code i am getting Different Result for both Variable.
as Below :
******Before [[Hello, Hiiii]]
******Before [Hello, Hiiii]
******After [[]]
******After []
can some one Please Help me to Understand where i am lacking here. i want to clear temp arraylist so that i can put the different Values for UESTION_ID_Of_SectionId_Temp so for second index of QuestionIdList_Section i will set the Value different.
Thanks in Advance.
QUESTION_ID_Of_SectionId_Temp.remove(item)instead ofQUESTION_ID_Of_SectionId_Temp.clear();QuestionIdList_Section.add(0,QUESTION_ID_Of_SectionId_Temp);toQuestionIdList_Section.add(0,new ArrayList<String>(QUESTION_ID_Of_SectionId_Temp));and your code will work because you will need to copy ArrayList instead of passing reference before cleaning it