关联数组和Java

sli*_*n77 10 java arrays multidimensional-array

我遇到了许多人似乎从PHP中遇到的同样问题,缺乏一个体面且易于使用的关联数组解决方案.我在这里阅读了一些基本上都建议使用HashMap的问题,比如Q:Java associative-array

但是,我认为所提到的解决方案不会解决我的问题.我会解释一下.

我有一个包含250个项目(国家/地区)的列表,我想要存储数据.数据长度未定义,这意味着它可以为每个"列"保留多个条目,有时没有条目,有时是4,等等.

在PHP中,我可以这样做:

$country_data = new array();
$country_data["nl"]["currency"] = "euro";
$country_data["nl"]["languages"] = "Dutch";
...
$country_data["us"]["currency"] = "US dollar";
$country_data["us"]["languages"] = array("English", "Spanish");
Run Code Online (Sandbox Code Playgroud)

所以有时我想存储一个数组,有时候不是.当然,它也可以是只有一个条目而不是字符串的数组,但我只是说.

所以,问题是,如何在HashMap中的数组中存储和获取数组?我知道我几乎坚持使用丑陋的HashMap解决方案,但我仍然无法看到这将如何让我存储数组,我敢肯定我会忽略一些简单的事情.基于我的一个例子会很棒!

UPDATE

我选择使用HashMaps的HashMaps原因我需要能够轻松监督所有内容,并在需要时更改几行值.这很灵活,我可以轻松地根据国家/地区代码,语言获取国家/地区名称,或者我可以在需要时获取country_data HashMap,或者所有国家/地区名称等等.

public class iso_countries {  
    Map<String, Object> country_data        = new HashMap<String, Object>();
    Map<String, String> country_name        = new HashMap<String, String>();
    Map<String, String[]> country_idd       = new HashMap<String, String[]>();
    Map<String, String[]> country_cid       = new HashMap<String, String[]>();

    public iso_countries(){      
         country_name.put("nl",         "Netherlands");      
         country_idd.put("nl",      new String[]{"+31"});
         country_cid.put("nl",      new String[]{"#31#", "*31#"});
         setData(country_name, country_cid, country_idd);
         // 249 * 4 lines more and later...
    }//end method

    public void setData(Map country_name, Map country_cid, Map country_idd){
         country_data.put("name",   country_name);
         country_data.put("idd",    country_idd);
         country_data.put("cid",    country_cid);
    }//end method   

    public String getCountryName(String countryCode){
        String name     = country_name.get(countryCode);
        return name;
    }//end method

    public String[] getCountryIdd(String countryCode){
        String prefix[] = country_idd.get(countryCode);
        return prefix;
    }//end method

    public String[] getCountryCid(String countryCode){
        String cid[]    = country_cid.get(countryCode);
        return cid;
    }//end method
}//end class
Run Code Online (Sandbox Code Playgroud)

kev*_*628 12

也许我误解了你的问题,但为什么不创建自己的对象来保存数据,然后将它们存储在HashMap中?

Java是一种面向对象的语言,所以为什么不使用它最着名的工具:对象!

  • 也许是因为经过多年的Java经验,您可以学习PHP并发现关联数组是您搜索过的对象.它们更酷,因为您只需从数据库中填充它们,并在GUI中直接访问它们.不需要Java数据模型,因此至少减少20%的工作量.你改变了你的洞数据库?没问题,如果旧的Java GUI使用关联数组,它仍会运行.您只需要从新数据库填充相同的数组.最后,它是关于创建哈希映射数组而不是像你提出的对象的哈希映射.编程是一门艺术.;) (4认同)

Pol*_*ome 11

PHP中的数组实际上是哈希,而不是数组.

在java中使用的正确的东西是HashMap.您还可以通过在其中存储数组或列表来将多重值存储在Hashmap中.所以HashMap<String, HashMap<String, Object>或者Hashmap<String, List<Object>>可能会帮助你.

使用多维数组时,必须使用整数作为键.


tsk*_*zzy 8

您可以将数组存储为以下值HashMap:

HashMap<Integer,String[]> hm = new HashMap<Integer,String[]>();
hm.put( 1, new String[]{ "a", "b" } );
Run Code Online (Sandbox Code Playgroud)

至于拥有"多维"键,你总是可以用一个类将它们包装在一起.另外,虽然难看,解决办法是将有HashMapHashMap秒.


Rei*_*ica 5

正确的方法是使用Country对象:

class Country {
    // Might want these private with getters and/or setters
    public String currency;
    public Set<String> languages;

    // String...languages is like String[] languages, except you can pass the
    // arguments in like new Country("Euro", "Dutch", "German")
    // instead of new Country("Euro", new String[] { "Dutch", "German" })
    // It's purely stylistic
    public Country(String currency, String... languages) {
        this.currency = currency;
        this.languages = new HashSet<String>();
        for(String language : languages) {
            this.languages.add(language);
        }
        // or this.languages = new HashSet<String>(Arrays.asList(languages));
    }
}

void someFunction() {
    Map<String, Country> countries = new HashMap<String, Country>():
    countries.put("us", new Country("US Dollar", "English", "Spanish"));
    countries.put("nl", new Country("Euro", "Dutch"));
}
Run Code Online (Sandbox Code Playgroud)

您可以通过嵌套列表和映射来实现此目的,如果您不打算使用编译器提供的工具,为什么要使用Java呢?