java 从 ArrayList<HashMap<String, String>> 取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16544928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:16:26  来源:igfitidea点击:

Take values from ArrayList<HashMap<String, String>>

javaandroidarraylisthashmap

提问by kareth

I have a problem about getting a value from my ArrayList<HashMap<String, String>>.

我在从我的ArrayList<HashMap<String, String>>.

My code is:

我的代码是:

ArrayList<HashMap<String, String>> myArrayList;

and then:

接着:

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
myArrayList.add(map);

If I want to get the name, for example, I tried as follow but I obtain a runtime error (the application crashes):

例如,如果我想获取名称,我尝试了以下操作,但出现运行时错误(应用程序崩溃):

System.out.println(myArrayList.get(1).get(TAG_NAME));

How can I solve it?

我该如何解决?

Thank you very much!

非常感谢你!

回答by zw324

System.out.println(myArrayList.get(1).get(TAG_NAME));

System.out.println(myArrayList.get(1).get(TAG_NAME));

ArrayListis 0-based. get(0)instead.

ArrayList是基于 0 的。get(0)反而。

回答by NullPointerException

You can loop through the list like this

您可以像这样遍历列表

   for(Map<String, String> map : myArrayList)
    {
        String tagName = map.get(TAG_NAME);
        System.out.println(tagNAme);
    }