-1

嗨,我的代码有问题。

String[] vArr = new String[4];
vArr[0] = "329~abc-2~PACKET_DATA_GATEWAY_IP_ADDRESS~1.2.3.4";
vArr[1] = "328~abc-1~PACKET_DATA_GATEWAY_IP_ADDRESS~1.1.1.1";
vArr[2] = "329~abc-2~PACKET_DATA_GATEWAY_PORT_NUMBER~123";
vArr[3] = "328~abc-1~PACKET_DATA_GATEWAY_PORT_NUMBER~123";

if(vArr != null && vArr.length > 0) {
    System.out.println("vArr.length ::" + vArr.length);                 
    ArrayList<VehicleTO> aprogList = new ArrayList<VehicleTO>();            

    for(int i = 0;i < vArr.length; i++) {
        System.out.println("Inside for loop ::" + vArr.length);
        String[] iData = vArr[i].split("~");
        System.out.println("value retrieved :" + Arrays.toString(iData));
        HashMap<Set, ArrayList<VehicleTO>> programValues = 
                             new HashMap<Set, ArrayList<VehicleTO>>();

        String programName = null;
        programName = iData[1];

        HashSet<String> listnerOutputArgList = new HashSet<String>();
        listnerOutputArgList.add(programName);
        Collections.unmodifiableSet(listnerOutputArgList);

        String progId = null;
        String paramName = null;
        String paramvalue = null;
        progId = iData[0];
        paramName = iData[2];
        paramvalue = iData[3];                  

        VehicleTO vehTO = new VehicleTO();                      
        vehTO.setProgramName(programName);

        if(paramName.equals("PACKET_DATA_GATEWAY_IP_ADDRESS")) {    
            vehTO.setIpAddress(paramvalue);         
        }
        System.out.println("Setting Values ip ::" + vehTO.getIpAddress());

        if(paramName.equals("PACKET_DATA_GATEWAY_PORT_NUMBER")) {   
            vehTO.setPortNumber(paramvalue);                
        }
        System.out.println("Setting Values port ::" + vehTO.getPortNumber());

        if(listnerOutputArgList.contains(vehTO.getProgramName())) {
            aprogList.add(vehTO);
        }
        int progDet = aprogList.size();

        System.out.println("The list value:"+ progDet);         

        programValues.put(listnerOutputArgList,aprogList);

        for (Iterator<Set> it = programValues.keySet().iterator(); it.hasNext()) {
            Set key = it.next();
            if(programValues.containsKey(key)) {
                programValues.get(key).add(vehTO);
                ArrayList<VehicleTO> value = programValues.get(key);

                for (int nCount = 0; nCount < value.size(); nCount++) {
                         System.out.println((String)value.get(nCount).getIpAddress());
                         System.out.println((String)value.get(nCount).getPortNumber());
                }
        } 
    }       
}

输出需要显示为:

abc-1   1.2.3.4  123
abc-2   1.1.1.1  123

但我的问题是列表值显示为:4 而不是 2,因为输出是错误的。

如何解决这个问题:

4

1 回答 1

0

我没有试图找到答案;最好先简化,使其更具可读性。我看到您对 java 7 仍然不太有信心,因此重写了一些代码:

使用Set<String>.

        for (Set<String> key : programValues.keySet()) {
             ArrayList<VehicleTO> value = programValues.get(key);
             value.add(vehTO);
             for (VehicleTO vTO : value) {
                 System.out.println((String)vTO.getIpAddress());
                 System.out.println((String)vTO.getPortNumber());
             }
         }
于 2012-11-27T11:12:06.250 回答