Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从HashMap获取值<;字符串,ArrayList<;字符串>&燃气轮机;_Java_Hashmap - Fatal编程技术网

Java 从HashMap获取值<;字符串,ArrayList<;字符串>&燃气轮机;

Java 从HashMap获取值<;字符串,ArrayList<;字符串>&燃气轮机;,java,hashmap,Java,Hashmap,[更新代码](抱歉,伙计们,我没有提供全部代码,因为根据我的经验,大型代码似乎“吓跑”了可能的帮助者。) 对于可扩展列表视图,我想构建一个HashMap,其中String是一个类别的名称,ArrayList是属于该类别的动物的名称。我按如下方式填充HashMap: HashMap<String, ArrayList<String>> map_groups_childs; ArrayList<String> list_group_titles; private

[更新代码](抱歉,伙计们,我没有提供全部代码,因为根据我的经验,大型代码似乎“吓跑”了可能的帮助者。)

对于可扩展列表视图,我想构建一个
HashMap
,其中
String
是一个类别的名称,
ArrayList
是属于该类别的动物的名称。我按如下方式填充HashMap:

HashMap<String, ArrayList<String>> map_groups_childs;
ArrayList<String> list_group_titles;

private void prepareListData(ArrayList<Id_triv_cat> search_results) {
    list_group_titles = new ArrayList<String>();                  // this is a list of group titles
    map_groups_childs = new HashMap<String, ArrayList<String>>(); // this is a map. each group title gets a list of its respective childs

    // temporary List of items for a certain category/group
    ArrayList<String> temp_childs_list = new ArrayList<String>();

    // "search_results" is an ArrayList of self defined objects each containing an ID, a name and a category name
    int count = search_results.size();
    int i_cat = 0;
    int i=0;

    // if category "i" is the same as the next category "i+1", add child to list
    for (i=0; i<count-1; i++) {
        // build group with category name
        list_group_titles.add(search_results.get(i).get_type());

        // while category does not change, add child to the temporary childs-array
        while (i<=count && search_results.get(i).get_type().equals(search_results.get(i+1).get_type())) {
            temp_childs_list.add(search_results.get(i).get_name());
            i++;
        }

        // must be done, as the while loop does not get to the last "i" of every category
        temp_childs_list.add(search_results.get(i).get_name());

        Log.i("DEBUG", temp_childs_list.size());      // --> returns always more than 0
        Log.i("DEBUG", temp_childs_list.toString());  // --> returns always something like [word1, word2, word3, ...]
        Log.i("DEBUG", list_group_titles.get(i_cat)); // --> returns always a single word like "Insekten"

        // add [group_title:list_of_childs] to map
        map_groups_childs.put(list_group_titles.get(i_cat++), temp_childs_list);

        // clear temp_list, otherwise former category's species will be added to new category
        temp_childs_list.clear();
    }
Log.i("DEBUG", map_groups_childs.containsKey("Insekten"));  // --> returns true
Log.i("DEBUG", map_groups_childs.size());                   // --> returns 10
Log.i("DEBUG", map_groups_childs.get("Insekten").size());   // --> returns 0
Log.i("DEBUG", map_groups_childs.toString());               // --> returns {Insekten=[], Reptilien=[], ...}
}
HashMap\u group\u childs;
ArrayList\u group\u标题;
私有void prepareListData(ArrayList搜索结果){
list_group_titles=new ArrayList();//这是组标题列表
map_groups_childs=new HashMap();//这是一个映射。每个组标题都会获得其各自子组的列表
//特定类别/组的临时项目列表
ArrayList temp_childs_list=新建ArrayList();
//“search_results”是自定义对象的数组列表,每个对象包含一个ID、一个名称和一个类别名称
int count=搜索结果.size();
int i_cat=0;
int i=0;
//如果类别“i”与下一个类别“i+1”相同,则将子类别添加到列表中
for(i=0;我总是返回类似于[word1,word2,word3,…]
Log.i(“DEBUG”,list_group_titles.get(i_cat));//-->始终返回一个单词,如“Insekten”
//将[组标题:儿童列表]添加到地图
map\u groups\u childs.put(list\u group\u titles.get(i\u cat++)、temp\u childs\u list);
//清除临时列表,否则前类别的物种将添加到新类别中
temp_childs_list.clear();
}
Log.i(“DEBUG”,map_groups_childs.containsKey(“Insekten”);//-->返回true
Log.i(“DEBUG”,map_groups_childs.size());//-->返回10
Log.i(“DEBUG”,map_groups_childs.get(“Insekten”).size();//-->返回0
Log.i(“DEBUG”,map_groups_childs.toString());//-->返回{Insekten=[],爬行动物=[],…}
}
在for-and-while循环中使用相同的
i
可能看起来是错误的或令人困惑的,但这没关系。没有任何
i
被以任何方式跳过或使用两次


我放在HashMap中的所有键都在那里,但是我想要使用的数组列表(例如)
map\u groups\u childs.get(“Insekten”)
是空的。我做错了什么?

我能告诉你需要的是下面第二组中的5行

    HashMap<String, ArrayList<String>> map_groups_childs = new HashMap<String, ArrayList<String>>();
    ArrayList<String> list_group_titles = new ArrayList<String>();
    ArrayList<String> temp_childs_list  = new ArrayList<String>();

    list_group_titles.add("insects");
    temp_childs_list.add("Swallowtail");
    temp_childs_list.add("Small White");
    temp_childs_list.add("Large White");
    temp_childs_list.add("Silverfish");

    int k = 0; 

    Log.i("DEBUG", list_group_titles.get(k)); // --> returns "insects"
    Log.i("DEBUG", temp_childs_list);         // --> returns [Swallowtail, Small White, Large White, Silverfish]

    map_groups_childs.put(list_group_titles.get(k), temp_childs_list);

    Log.i("DEBUG", map_groups_childs.size());                 // --> returns 1 not 10
    Log.i("DEBUG", map_groups_childs.containsKey("insects")); // --> returns true
    Log.i("DEBUG", map_groups_childs.get("insects").size());  // --> returns 4 not 0
HashMap\u groups\u childs=newhashmap();
ArrayList\u group\u titles=新建ArrayList();
ArrayList temp_childs_list=新建ArrayList();
列出组标题。添加(“昆虫”);
临时儿童列表。添加(“燕尾型”);
临时儿童列表。添加(“小白”);
临时儿童列表。添加(“大白色”);
临时儿童名单。添加(“银鱼”);
int k=0;
Log.i(“DEBUG”,list_group_titles.get(k));//-->返回“昆虫”
Log.i(“DEBUG”,temp_childs_list);//-->返回[燕尾、小白、大白、银鱼]
map\u groups\u childs.put(list\u group\u titles.get(k)、temp\u childs\u list);
Log.i(“DEBUG”,map_groups_childs.size());//-->返回1而不是10
Log.i(“DEBUG”,map_groups_childs.containsKey(“昆虫”);//-->返回true
Log.i(“DEBUG”,map_groups_childs.get(“昆虫”).size();//-->返回4而不是0

即使进行了编辑,您的代码仍然缺少关于正在发生的事情的大量线索。我做了一些猜测,如果我是对的,您这样做太难了

我推断出存在一个类
Id\u triv\u cat
,该类的getter名为
get\u name()
get\u type()

static class Id_triv_cat {

    String type;
    String name;

    Id_triv_cat( String type, String name )
    {
        this.name = name;
        this.type = type;
    }

    public String get_type()
    {
        return type;
    }
    public String get_name()
    {
        return name;
    }
}
我还编写了这段代码来测试您的
prepareListData()
方法

public static void main(String[] args){
    ArrayList<Id_triv_cat> search_results = new ArrayList<Id_triv_cat>();
    search_results.add( new Id_triv_cat("type1", "name1") );
    search_results.add( new Id_triv_cat("type2", "name2") );
    search_results.add( new Id_triv_cat("Insekten", "insekten name1") );
    search_results.add( new Id_triv_cat("Insekten", "insekten name2") );
    search_results.add( new Id_triv_cat("type3", "name3") );
    new Test().myPrepareListData( search_results );
}
这将显示:
my_map\u groups\u childs={Insekten=[Insekten name1,Insekten name2],type1=[name1],type3=[name3],type2=[name2]}

看看几个精心挑选的当地人如何让生活变得如此轻松

如果这不是你想要的,你就必须让你的问题更清楚

Radiodef是对的。当您用Java编写代码时,您真的应该使用camelCase

    ...

    map_groups_childs.put(..., temp_childs_list);

    temp_childs_list.clear();
}
对象在Java中作为引用传递。您总是将相同的列表放入映射中,并在每次迭代后清除它。因此,映射中的每个值都指向相同的空列表

您可能需要的是以下内容:

for( ... ) {
    List<String> tempChildsList = new ArrayList<>();

    ...

    mapGroupChilds.put(..., tempChildsList);
}
(…)的
{
List tempChildsList=newarraylist();
...
mapGroupChilds.put(…,tempChildsList);
}
因此,每次迭代都会创建一个新列表

我也同意@CandiedOrange的观点,你的代码乱七八糟,可能过于复杂。一般来说,像List和Map这样的抽象概念的要点是不要通过一直计算数字索引来访问东西


请注意,在Java中,惯例是变量的标识符是camelCase,而不是分数不足。

错误出现在您选择不显示的代码中。您的示例代码是无用的,因为它没有给出任何提示,您的问题可能是。也许您添加了一个新生成的arraylist,而不是添加填充的arraylist。我没有提供全部代码,因为根据我的经验,大型代码似乎“吓跑”了可能的帮助者。"不,你应该做的是通过将问题简化为最简单的形式来创建一个。camelCase为+1。下划线适用于所有像这样的cap期末考试。就是这样!不用说,我在Java方面不是专家。引用的概念通常对我来说并不重要,这就是为什么我从来没有见过这个。你的如果oach和我的一样的话,它确实要简单得多。通常我倾向于避免for each循环,因为我发现它们很难阅读,但我会尝试并尝试理解您的代码。如果正确使用,for each是一个很棒的抽象。然而,我也喜欢使用索引。但我已经学会了克制,现在在我有理由的时候使用它们我需要得到索引。如果我转换为使用索引,我在这里展示的代码只需要再多一行。实际上,它是局部变量(类型、名称、,
for( ... ) {
    List<String> tempChildsList = new ArrayList<>();

    ...

    mapGroupChilds.put(..., tempChildsList);
}