HashMap accessing same ArrayList
I am trying the following to update the HashMap
with new value of student, please check where I'm missing something.
ArrayList<Student> tempStudentList =开发者_Python百科 XMLParser.studentHashMap.get(currentSectionName);
if(studentList==null)
{
Log.v(CURRENT_SCREEN,"created another student list for section name:"+currentSectionName);
tempStudentList = new ArrayList<Student>();
}
Log.v(CURRENT_SCREEN,"Added student to the list");
tempStudentList.add(currentStudent);
XMLParser.studentHashMap.put(currentSectionName, tempStudentList);
I think you wanted to write tempStudentList
instead of studentList
at the null check.
You're checking the wrong variable for null. Also, Java collections are mutable, the array in studentHashMap is manipulated directly so you don't need to perform another put after every query on your map:
ArrayList<Student> tempStudentList = XMLParser.studentHashMap.get(currentSectionName);
if(tempStudentList == null)
{
Log.v(CURRENT_SCREEN, "created another student list for section name:"+currentSectionName);
tempStudentList = new ArrayList<Student>();
XMLParser.studentHashMap.put(tempStudentList);
}
tempStudentList.add(currentStudent);
Log.v(CURRENT_SCREEN,"Added student to the list");
精彩评论