简体   繁体   English

从 ArrayList 访问数据

[英]Accessing Data From ArrayList

I m working on populating a list dynamically with dynamic rows and columns and showing them in UI.我正在使用动态行和列动态填充列表并在 UI 中显示它们。 All have been done accurately but im unable to get value from a specified row and column eg row 4 and column 3. Code is as under:-一切都已准确完成,但我无法从指定的行和列中获取值,例如第 4 行和第 3 列。代码如下:-

    public LiveRangeService() {
    tableData = new ArrayList< Map<String, ColumnModel> >();

    //Generate table header.
    tableHeaderNames = new ArrayList<ColumnModel>();
    for (int j = 0; j < 10; j++) {
          tableHeaderNames.add(new ColumnModel("header "+j, " col:"+ String.valueOf(j+1)));
    }

    //Generate table data.
    for (int i = 0; i < 8; i++) {
        Map<String, ColumnModel> playlist = new HashMap<String, ColumnModel>();
        for (int j = 0; j < 10; j++) {
            playlist.put(tableHeaderNames.get(j).key, new ColumnModel(tableHeaderNames.get(j).key,"row:" + String.valueOf(i+1) +" col:"+ String.valueOf(j+1)));
        }
        tableData.add(playlist);
    }
    try {
        System.out.println( "Table Data Size " + tableData.size() );
        System.out.println( "Value Of Row 1 Col 2: " + tableData.get(1).get(2) );
    } catch (Exception e) {
        System.out.println( "Error!! " + e.getMessage() );
    }
}

may some body pls help me in understanding this chunk of code.可以请一些人帮助我理解这段代码。 Error occurs on the following line:-错误发生在以下行:-

            System.out.println( "Value Of Row 1 Col 2: " + tableData.get(1).get(2) );

the result shown on page页面上显示的结果

tableData = new ArrayList< Map<String, ColumnModel> >();

you are storing maps in list and this map is of generic means key in map is String.您将地图存储在列表中,并且此 map 是通用手段,map 中的键是字符串。

tableData.get(1).get(2)

This above line get(1) will give you first Map object.上面这行get(1)将首先给您 Map object。 then get(2) , means you are passing Integer as key to fetch value from map but your map needs String as key.然后get(2) ,意味着您将Integer作为从 map 获取值的键传递,但您的 map 需要String作为键。

Its map and value can be retrieved using key and not based on index like arrayList.它的 map 和值可以使用键检索,而不是基于像 arrayList 这样的索引。 so you are getting error所以你得到错误

pass something like通过类似的东西

tableData.get(1).get(<some string key>) 

it should work它应该工作

Java counts starting from 0. Java 从 0 开始计数。

tableData.get(0).get(0) will get the element at row 1, and column 1. The ArrayList containing Map s starts from 0. However, tableData.get(tableData.size()) will throw an error, as it will contain the number of elements in the ArrayList. tableData.get(0).get(0)将获取第 1 行和第 1 列的元素。包含MapArrayList从 0 开始。但是, tableData.get(tableData.size())会抛出错误,因为它将包含 ArrayList 中的元素数量。 (So, if an array had 3 elements, tableData.size() would return 3 and you would be able to call for it by calling for tableData.get(2) ) (因此,如果一个数组有 3 个元素, tableData.size()将返回 3 并且您可以通过调用tableData.get(2)来调用它)

tableData.get(1).get(2) will get the element at row 2, and column 3. tableData.get(1).get(2)将获取第 2 行第 3 列的元素。

To get the value of the element at row 1, column 2, call tableData.get(0).get(1) .要获取第 1 行第 2 列元素的值,请调用tableData.get(0).get(1)

Fianlly with the help of LHCHIN, the key was header 0, header 1 etc and it worked great.最后在 LHCHIN 的帮助下,密钥是 header 0、header 1 等,效果很好。 finally i wrote: tableData.get(1).get("header 2").getValue() and it worked perfectly.最后我写道: tableData.get(1).get("header 2").getValue() 它运行良好。

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

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