如何从ArrayList中迭代此结构

时间:2014-11-07 07:02:44

标签: java servlets arraylist

我是Java的新手,我想知道如何迭代这个ArrayList

[
    [{ID_CLIENTE=1}, {CODIGO=001}, {NOMBRES=John}, {STA=1}], 
    [{ID_CLIENTE=2}, {CODIGO=003}, {NOMBRES=Laura },  {STA=1}]
]

这是一个arraylist,其中包含每行的记录,并且内部有一个值/列的列名和值,并且只绘制列数组上的选定列。 这是我的Java代码:

String columns = request.getParameter("columns");
String[] cols =  columns.split(",");
List<ArrayList<String>> mant =  mantDao.getAllMant(); // this return the arraylist shown above

    for(ArrayList<String> row: mant){
          //IF it is ON cols array 
          // response.getWriter().write("<tr>td>"+row.get(index) +"</td>"....); <-- BAD CODE    

    }

任何帮助都会得到真正的赞赏。我一直坚持这一点。

编辑: 这是getAllMant()函数

public List getAllMant() {        
    ArrayList mant = new ArrayList<ArrayList<String>>();
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(this.query);

        campos = this.getCampos();
            while (rs.next()) {
        ArrayList row = new ArrayList<>();
        for(List<String> campo: campos){
            LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
            String valueField = "";
            if( Arrays.asList(INTS).contains(Integer.parseInt(campo.get(1)) ) ){
            valueField = String.valueOf(rs.getInt(campo.get(0)));
            }else if( Arrays.asList(NUMBERS).contains(Integer.parseInt( campo.get(1)) ) ){
            valueField = String.valueOf(rs.getLong(campo.get(0)));
            }else if( Arrays.asList(VARCHAR).contains(Integer.parseInt( campo.get(1)) ) ){
            valueField = rs.getString(campo.get(0)) ;
            }else if( Arrays.asList(DATES).contains(Integer.parseInt( campo.get(1)) ) ){
            if(rs.getDate(campo.get(0)) !=null){
                valueField = rs.getDate(campo.get(0)).toString();
            }else{
                valueField = "";
            }
            }
            lhm.put(campo.get(0),valueField);
            row.add(lhm);
        }
        mant.add(row);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return mant;
    }

问候

1 个答案:

答案 0 :(得分:0)

List<ArrayList<String>>替换为List<Map<String,String>>会更有意义,因为Map更适合存储键/值对。

然后你的代码看起来像这样:

for(Map<String,String> row: mant){
      String id = row.get("ID_CLIENTE"); // fetch a value by its key
}

查看您发布的其他代码,您似乎正在创建ArrayList<ArrayList<String>>,但是您尝试在内部列表中放置LinkedHashMap<String,String>。这毫无意义,也无济于事。 您应该使用Map替换内部List:

public List getAllMant() 
{        
    ArrayList<Map<String,String>> mant = new ArrayList<Map<String,String>>();
    try {
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(this.query);

        campos = this.getCampos();
        while (rs.next()) {
            LinkedHashMap<String, String> row = new LinkedHashMap<String, String>();
            for(List<String> campo: campos){            
                String valueField = "";
                if( Arrays.asList(INTS).contains(Integer.parseInt(campo.get(1)) ) ){
                    valueField = String.valueOf(rs.getInt(campo.get(0)));
                } else if( Arrays.asList(NUMBERS).contains(Integer.parseInt( campo.get(1)) ) ){
                    valueField = String.valueOf(rs.getLong(campo.get(0)));
                } else if( Arrays.asList(VARCHAR).contains(Integer.parseInt( campo.get(1)) ) ){
                    valueField = rs.getString(campo.get(0)) ;
                } else if( Arrays.asList(DATES).contains(Integer.parseInt( campo.get(1)) ) ){
                    if(rs.getDate(campo.get(0)) !=null){
                        valueField = rs.getDate(campo.get(0)).toString();
                    } else{
                        valueField = "";
                    }
                }
                row.put(campo.get(0),valueField);
            }
            mant.add(row);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return mant;
}