解析为Java ArrayList对
我在Java中遇到了一个我试图解决的问题,但直到现在我还没有解决。通过下面的代码,我得到了控制台中XML解析器的响应;这一切都在一行中,就像这样:解析为Java ArrayList对,java,arrays,parsing,arraylist,multidimensional-array,Java,Arrays,Parsing,Arraylist,Multidimensional Array,我在Java中遇到了一个我试图解决的问题,但直到现在我还没有解决。通过下面的代码,我得到了控制台中XML解析器的响应;这一切都在一行中,就像这样: [359710042040320,铃木SX4“BB71521”,359710042067463,雪佛兰太浩黑“示范”,359710042091273,五十铃D'Max AA-08612,359710042110768,丰田4 Runner] 但我的目标是得到一个类似于成对数组列表的响应,其中每个设备ID和每个描述都放在一起,用逗号分隔 (Device
[359710042040320,铃木SX4“BB71521”,359710042067463,雪佛兰太浩黑“示范”,359710042091273,五十铃D'Max AA-08612,359710042110768,丰田4 Runner]
但我的目标是得到一个类似于成对数组列表的响应,其中每个设备ID和每个描述都放在一起,用逗号分隔
(DeviceID) (Descripcion)
359710042040320, Suzuki
359710042067463, Chevrolet
不要使用
列表
尝试使用哈希映射
。要定义它,您需要执行以下操作:
HashMap<String, String> result = new HashMap<String,String>();
现在,您可以通过名称(键)从地图访问您的值:
如果您需要查看更多文档:,正如Dott Bottstein所说,HashMap可能就是您想要的。我将使用LinkedHashMap,因为LinkedHashMaps保持原始顺序,而HashMaps根本不保证顺序 以下是您可能会做的:
Map<String, String> resultMap = new LinkedHashMap<String, String>();
for (int i = 0; i < nodeList.getLength(); i++) {
String deviceID = nodeList.item(i).getFirstChild().getNodeValue();
String descripcion = nodeList.item(i).getAttributes().getNamedItem("name").toString();
resultMap.put(deviceID, descripcion);
}
//ok, lets print out what's in the Map
Iterator<String> iterator = resultMap.keySet().iterator();
while(iterator.hasNext()){
String deviceID = iterator.next();
String descripcion = resultMap.get(key);
System.out.println(deviceID + ", " + descripcion);
}
Maps have the big advantage that afterwards you can look up a descripcion very quickly if you have the deviceID.
然后创建车辆实例列表:
List<Vehicle> result = new ArrayList<Vehicle>();
for (int i = 0; i < nodeList.getLength(); i++) {
String deviceID = nodeList.item(i).getFirstChild().getNodeValue();
String descripcion = nodeList.item(i).getAttributes().getNamedItem("name").toString();
result.add(new Vehicle(deviceID, descripcion));
}
List result=new ArrayList();
for(int i=0;i
最后,您可能希望将ID保留为一个长数字,而不是字符串。这对于
HashMap
或List
idea来说不是问题,但它不适用于List
idea。HashMaps可以很好地使用长密钥。键必须是一个长对象,但Java自动从长对象转换为长对象,因此您甚至不必考虑它,只需设置一个长原语作为键,它就会工作。HashMap更适合。我尽了最大努力解决它,但在我看来,它非常困难,如果有人能帮助我,我将非常感激。如何尝试使用HashMap?我希望其中一个能够回答您的问题。如果是这样,我们将非常感谢您的接受!谢谢
Map<String, String> resultMap = new LinkedHashMap<String, String>();
for (int i = 0; i < nodeList.getLength(); i++) {
String deviceID = nodeList.item(i).getFirstChild().getNodeValue();
String descripcion = nodeList.item(i).getAttributes().getNamedItem("name").toString();
resultMap.put(deviceID, descripcion);
}
//ok, lets print out what's in the Map
Iterator<String> iterator = resultMap.keySet().iterator();
while(iterator.hasNext()){
String deviceID = iterator.next();
String descripcion = resultMap.get(key);
System.out.println(deviceID + ", " + descripcion);
}
Maps have the big advantage that afterwards you can look up a descripcion very quickly if you have the deviceID.
static int DEVICE_ID = 0;
static int DESCRIPCION = 1;
List<String[]> result = new ArrayList<String[]>();
for (int i = 0; i < nodeList.getLength(); i++) {
String[] vehicleArray = new String[2];
vehicleArray[DEVICE_ID] = nodeList.item(i).getFirstChild().getNodeValue();
vehicleArray[DESCRIPCION] = nodeList.item(i).getAttributes().getNamedItem("name").toString();
result.add(vehicleArray);
}
class Vehicle{
String deviceID;
String descripcion;
public Vehicle(String deviceID, String descripcion){
this.deviceID = deviceID;
this.descripcion = descripcion;
}
}
List<Vehicle> result = new ArrayList<Vehicle>();
for (int i = 0; i < nodeList.getLength(); i++) {
String deviceID = nodeList.item(i).getFirstChild().getNodeValue();
String descripcion = nodeList.item(i).getAttributes().getNamedItem("name").toString();
result.add(new Vehicle(deviceID, descripcion));
}