简体   繁体   中英

Parsing into a Java ArrayList pair

I have a problem in Java that I tried to solve, and until now I can't get it. With this code below I have a response from an XML Parser in the console; it's all in one line like this:

[359710042040320, Suzuki SX4 "BB71521", 359710042067463, Chevrolet Tahoe Noir "Demonstration", 359710042091273, Isuzu D'Max AA-08612, 359710042110768, Toyota 4 Runner]

But my objective is to get a response like an ArrayList of pairs, where each Device ID and each Description are together, separated by a comma.

(DeviceID)            (Descripcion)
359710042040320, Suzuki
359710042067463, Chevrolet

Instead of using a List<String> try using a HashMap<String, String> . To define it you would do:

HashMap<String, String> result = new HashMap<String,String>();

Then inside your loop replace result.add(value) with:

result.put(name,value);

Now you can access your values from your map via the name(key):

result.get(name);//Note Name is a string that holds you key value

If you need to see more documentation : HashMap Documentation

As Dott Bottstein said a HashMap might be what you are looking for. I'll use a LinkedHashMap because LinkedHashMaps keep the original order, whereas HashMaps do not guarantee order at all.

Here is what you might do:

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.

If you really want an ArrayList you could do it in two ways:

1) an ArrayList of String[] arrays with a length of 2

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);
}

or 2) you could create a class to hold the vehicle data:

class Vehicle{

    String deviceID;
    String descripcion;

    public Vehicle(String deviceID, String descripcion){
        this.deviceID = deviceID;
        this.descripcion = descripcion;
    }

}

And then create a List of Vehicle instances:

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));
}

Finally you might actually like to keep the ID as a long number instead of a String. That's not a problem for the HashMap or the List<Vehicle> idea, but it wouldn't work with the List<String[]> idea. HashMaps work very well with a key that's a Long. The key must be a Long Object, but Java automagically converts from longs to Long objects, so you don't even have to think about it, just set a long primitive as the key and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM