Java HashMap关联多维数组无法创建或添加元素

好的,所以我花了几个小时试图用Java中的HashMap这个概念来解决问题,但只是无法弄清楚。我看过许多教程,但似乎都没有一个能满足我的确切要求,我无法使其正常运行。

我试图用Java(或类似的东西)创建一个关联的多维数组,以便我既可以使用字符串键又可以保存到数组或从数组中检索。

这就是我将在PHP中执行的操作,并最好地解释了我要执行的操作:

//loop one - assign the names

myArray['en']['name'] = "english name";

myArray['fr']['name'] = "french name";

myArray['es']['name'] = "spanish name";

//loop two - assign the description

myArray['en']['desc'] = "english description";

myArray['fr']['desc'] = "french description";

myArray['es']['desc'] = "spanish description";

//loop three - assign the keywords

myArray['en']['keys'] = "english keywords";

myArray['fr']['keys'] = "french keywords";

myArray['es']['keys'] = "spanish keywords";

//later on in the code be able to retrive any value similar to this

english_name = myArray['en']['name'];

french_name = myArray['fr']['name'];

spanish_name = myArray['es']['name'];

这是我在Java中尝试过的方法,但无法正常工作:

HashMap<String, HashMap<String, String>> myArray = new HashMap<String, HashMap<String, String>>();

myArray.put("en" , put("name", "english name")); //gives me "cannot find symbol" at second put

myArray.put("en" , ("name", "english name")); //gives me "')' expected" after second comma

因此,我敢肯定我很想念它,但请指出来,因为这很令人沮丧!

谢谢

所以这是一些有关如何实现我接受的答案的工作代码:

import java.util.*;

HashMap<String, HashMap<String, String>> finalArray = new HashMap<String, HashMap<String, String>>();

String[] langArray = {"en","fr","de","no","es"};

//Initialize each language key ahead of time

for(String lang : langArray) { // foreach lang in langArray

if (!finalArray.containsKey(lang)) {

finalArray.put(lang, new HashMap<String, String>());

}

}

//loop one - assign names

for(String lang : langArray) {

String theName = lang + " name"; //go get the name from somewhere

finalArray.get(lang).put("name", theName);

}

//loop two - assign description

for(String lang : langArray) {

String theDesc = lang + " description"; //go get the description from somewhere

finalArray.get(lang).put("desc", theDesc);

}

//loop three - assign keywords

for(String lang : langArray) {

String theKeys = lang + " keywords"; //go get the keywords from somewhere

finalArray.get(lang).put("keys", theKeys);

}

//display output

for(String lang : langArray) {

System.out.println("LANGUAGE: " + lang);

System.out.println(finalArray.get(lang).get("name"));

System.out.println(finalArray.get(lang).get("desc"));

System.out.println(finalArray.get(lang).get("keys"));

}

//example to retrieve/get values

String english_name = finalArray.get("en").get("name");

String french_desc = finalArray.get("fr").get("desc");

回答:

HashMap<String, HashMap<String, String>> myArray = new HashMap<String, HashMap<String, String>>();

if (!myArray.containsKey("en")) {

myArray.put("en", new HashMap<String, String>());

}

myArray.get("en").put("name", "english name");

在Java中,必须明确说明何时创建对象。在这种情况下,首先我们检查是否在键“

en”下的HashMap外部存储了一个对象HashMap。如果没有,我们将创建一个空的。

现在要添加一个新值,我们必须首先从外部获取它HashMap,然后再添加新值。

以上是 Java HashMap关联多维数组无法创建或添加元素 的全部内容, 来源链接: utcz.com/qa/399455.html

回到顶部