arraylist也没有在散列表中存储
即时解析xml在其中的特定指定内容列表... 因此,当解析我usee散列表来存储value.I继续在arraylist中添加staffdata,当我发现结束元素标记我把在HashMap中显示ArrayList对象并清除Arraylist以存储下一个数据。arraylist也没有在散列表中存储
但是当我打印HashMap中,我发现只有在HashMap的关键是没有的ArrayList在所有...
如果我在HashMap中那么值在HashMap中发现puting ArrayList中后不明确的ArrayList ......但所有值来together..so我必须在哈希表放数组列表后清除数组列表...
**HERE IS MY CODE OF XML PARSING**
package com.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class StaffXmlHandler extends DefaultHandler
{
static int totalSection=0;
static HashMap<String,ArrayList<Staff>> staffListWithDesignation=new HashMap<String,ArrayList<Staff>>();
ArrayList<Staff> staffList=new ArrayList<Staff>();
String designationName;
public static HashMap<String,ArrayList<Staff>> getStaffListWithDesignation()
{
return staffListWithDesignation;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if(localName.equals("designation"))
{
designationName= attributes.getValue("name");
System.out.println("=====================>>>>Designation="+designationName);
}
else if (localName.equals("staff"))
{
//System.out.println("======>>>>fetching data from staff");
String employeeName = attributes.getValue("name");
String employeeEmail=attributes.getValue("email");
String employeePost=attributes.getValue("designation");
String employeePhone=attributes.getValue("phone");
Staff s=new Staff();
s.setEmployeeName(employeeName);
s.setEmployeeEmail(employeeEmail);
s.setEmployeePhone(employeePhone);
s.setEmployeePost(employeePost);
staffList.add(s);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
/** set value */
if (localName.equalsIgnoreCase("designation"))
{
// Staff[] s=new Staff[staffList.size()];
// System.out.println("=========================="+designationName+"=======================");
// for(int i=0;i<staffList.size();i++)
// {
// System.out.println("=====Record "+(i+1)+"=====");
// s[i]=new Staff();
// s[i]=staffList.get(i);
// System.out.println("Name:"+s[i].getEmployeeName());
// System.out.println("email:"+s[i].getEmployeeEmail());
//
//
// }
ArrayList<Staff> temp=staffList;
staffListWithDesignation.put(designationName, temp);
staffList.clear();
designationName=null;
}
}
public void endDocument()
{
System.out.println("==================End Element tag");
Iterator<String> iterator =staffListWithDesignation.keySet().iterator();
while (iterator.hasNext())
{
String key = (String) iterator.next();
System.out.println("=====> " + key + "======" + StaffXmlHandler.staffListWithDesignation.get(key));
}
}
@Override
public void characters(char[] ch, int start, int length)throws SAXException
{
}
}
在上面代码,请在注释行如果我检查,看看WHEATHER ArrayList的CONTAIN DAA OR NOT话,就说明我数据但不知道为什么它不存储在HASHMAP .....
个PLS给我一些解决方案...
在此先感谢
后你把你的名单分成地图创建一个新的列表。当你把你的列表放入地图中时,它不会复制它,所以你对列表的进一步操作(添加更多东西或清除它)发生在同一个列表中。
像@kett_chup说,你不应该再使用你把在地图后ArrayList中,你应该分配一个新的列表,就像这样:
staffListWithDesignation.put(designationName, staffList);
staffList = new ArrayList<Staff>();
designationName = null;
的staffList属性将是可访问其他方法,就像它在第一位。
是的朋友....我做同样的事情,我的问题解决了....感谢你guyz ... – 2011-04-02 09:33:25
如果我将创建新的ArrayList后,将值放入hashmap中... ArrayList将不能从其他方法访问,它将数据添加到arraylist中 – 2011-04-02 08:12:25
好吧,很难在上面的代码片段中看到它。请整理一下。 – katsharp 2011-04-02 08:15:31
非常感谢你的建议..... – 2011-04-02 08:16:55