开发者

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜