简体   繁体   English

如何从ArrayList迭代此结构

[英]How do I can Iterate this structure from ArrayList

I am pretty new on Java, and I'm wondering how to iterate this ArrayList 我对Java很陌生,我想知道如何迭代此ArrayList

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

this is an arraylist that contains a record on each row, and inside there is a value/pair for the column name and value, and draw only the selected columns on the column array. 这是一个在每行上包含一条记录的arraylist,并且在其中有一个用于列名和值的值/对,并且仅在列数组上绘制选定的列。 This is my Java Code: 这是我的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    

    }

Any help will be really apprecciated. 任何帮助将不胜感激。 I'm stuck on this for while. 我坚持了一段时间。

EDIT: this is the getAllMant() function 编辑:这是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;
    }

regards 问候

It would make more sense to replace the List<ArrayList<String>> with a List<Map<String,String>> , since Map is better suited for storage of key/value pairs. List<Map<String,String>>替换List<ArrayList<String>>会更有意义,因为Map更适合存储键/值对。

Then your code could look like this : 然后您的代码可能如下所示:

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

Looking at the additional code you posted, it seems you are creating an ArrayList<ArrayList<String>> , but you try to put a LinkedHashMap<String,String> in the inner list. 查看您发布的其他代码,似乎您正在创建ArrayList<ArrayList<String>> ,但是您尝试将LinkedHashMap<String,String>放入内部列表中。 This makes no sense, and won't work. 这没有任何意义,也行不通。 You should replace the inner List with a Map : 您应该将内部List替换为Map:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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