如何制作哈希图数组?

发布于 2024-09-01 03:13:38 字数 209 浏览 10 评论 0 原文

这就是我试图做的,但它给了我一个警告:

HashMap<String, String>[] responseArray = new HashMap[games.size()];

类型安全:HashMap[ ]类型的表达式需要未经检查的转换才能符合HashMap[ ]

This is what I tried to do, but it gives me a warning:

HashMap<String, String>[] responseArray = new HashMap[games.size()];

Type safety: The expression of type HashMap[ ] needs unchecked conversion to conform to HashMap[ ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

伤感在游骋 2024-09-08 03:13:39

你可以使用这样的东西:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class testHashes {

public static void main(String args[]){
    Map<String,String> myMap1 = new HashMap<String, String>();

    List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();

    myMap1.put("URL", "Val0");
    myMap1.put("CRC", "Vla1");
    myMap1.put("SIZE", "Val2");
    myMap1.put("PROGRESS", "Val3");

    myMap.add(0,myMap1);
    myMap.add(1,myMap1);

    for (Map<String, String> map : myMap) {
        System.out.println(map.get("URL"));
        System.out.println(map.get("CRC"));
        System.out.println(map.get("SIZE"));
        System.out.println(map.get("PROGRESS"));
    }

    //System.out.println(myMap);

}


}

You can use something like this:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class testHashes {

public static void main(String args[]){
    Map<String,String> myMap1 = new HashMap<String, String>();

    List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();

    myMap1.put("URL", "Val0");
    myMap1.put("CRC", "Vla1");
    myMap1.put("SIZE", "Val2");
    myMap1.put("PROGRESS", "Val3");

    myMap.add(0,myMap1);
    myMap.add(1,myMap1);

    for (Map<String, String> map : myMap) {
        System.out.println(map.get("URL"));
        System.out.println(map.get("CRC"));
        System.out.println(map.get("SIZE"));
        System.out.println(map.get("PROGRESS"));
    }

    //System.out.println(myMap);

}


}
走过海棠暮 2024-09-08 03:13:39

Java 语言规范第 15.10 节规定:

数组创建表达式创建
一个新数组对象,其
元素的类型由
PrimitiveType 或
类或接口类型。它是一个
编译时错误,如果
ClassOrInterfaceType 并不表示
具体化类型(§4.7)。

上面的规则意味着该元素
输入数组创建表达式
不能是参数化类型,其他
比无限制的通配符。

您可以做的最接近的是使用未经检查的强制转换,无论是来自原始类型(如您所做的那样)还是来自无界通配符:

 HashMap<String, String>[] responseArray = (Map<String, String>[]) new HashMap<?,?>[games.size()];

您的版本显然更好:-)

The Java Language Specification, section 15.10, states:

An array creation expression creates
an object that is a new array whose
elements are of the type specified by
the PrimitiveType or
ClassOrInterfaceType. It is a
compile-time error if the
ClassOrInterfaceType does not denote a
reifiable type (§4.7).

and

The rules above imply that the element
type in an array creation expression
cannot be a parameterized type, other
than an unbounded wildcard.

The closest you can do is use an unchecked cast, either from the raw type, as you have done, or from an unbounded wildcard:

 HashMap<String, String>[] responseArray = (Map<String, String>[]) new HashMap<?,?>[games.size()];

Your version is clearly better :-)

毅然前行 2024-09-08 03:13:39

您不能拥有泛型类型的数组。请改用List

You can't have an array of a generic type. Use List instead.

复古式 2024-09-08 03:13:39

Java 不希望你创建一个 HashMap 数组,但它会让你创建一个对象数组。因此,只需在 HashMap 周围编写一个类声明作为外壳,并创建该类的一个数组。如果您愿意的话,这可以让您存储一些关于 HashMap 的额外数据——考虑到您已经有一个有点复杂的数据结构,这可能是一个好处。

这看起来像什么:

private static someClass[] arr = new someClass[someNum];

public class someClass {

private static int dataFoo;
private static int dataBar;
private static HashMap<String, String> yourArray;

...

}

Java doesn't want you to make an array of HashMaps, but it will let you make an array of Objects. So, just write up a class declaration as a shell around your HashMap, and make an array of that class. This lets you store some extra data about the HashMaps if you so choose--which can be a benefit, given that you already have a somewhat complex data structure.

What this looks like:

private static someClass[] arr = new someClass[someNum];

and

public class someClass {

private static int dataFoo;
private static int dataBar;
private static HashMap<String, String> yourArray;

...

}
还不是爱你 2024-09-08 03:13:39

关于@alchemist的答案,我仅使用HashMap和ArrayList添加了一些修改:

import java.util.ArrayList;
import java.util.HashMap;
public class ArrayOfHash {

public static void main(String[] args) {
    HashMap<String,String> myMap = new HashMap<String, String>();

    ArrayList<HashMap<String , String>> myArrayMap = new ArrayList<HashMap<String,String>>();

    myMap.put("Key1",   "Val0");
    myMap.put("Key2",   "Val1");
    myMap.put("Key3",   "Val2");
    myMap.put("Key4",   "Val3");

    myArrayMap.add(myMap);
    myArrayMap.add(myMap);

    for (int i = 0; i < myArrayMap.size(); i++) {
        System.out.println(myArrayMap.get(i).get("Key1") + ","
            + "" + myArrayMap.get(i).get("Key2") + ","
            + "" + myArrayMap.get(i).get("Key3") + ","
            + "" + myArrayMap.get(i).get("Key4"));
        
        System.out.println(); // used as new blank line
    }
}

Regarding the @alchemist's answer, I added some modifications using only HashMap and ArrayList:

import java.util.ArrayList;
import java.util.HashMap;
public class ArrayOfHash {

public static void main(String[] args) {
    HashMap<String,String> myMap = new HashMap<String, String>();

    ArrayList<HashMap<String , String>> myArrayMap = new ArrayList<HashMap<String,String>>();

    myMap.put("Key1",   "Val0");
    myMap.put("Key2",   "Val1");
    myMap.put("Key3",   "Val2");
    myMap.put("Key4",   "Val3");

    myArrayMap.add(myMap);
    myArrayMap.add(myMap);

    for (int i = 0; i < myArrayMap.size(); i++) {
        System.out.println(myArrayMap.get(i).get("Key1") + ","
            + "" + myArrayMap.get(i).get("Key2") + ","
            + "" + myArrayMap.get(i).get("Key3") + ","
            + "" + myArrayMap.get(i).get("Key4"));
        
        System.out.println(); // used as new blank line
    }
}
游魂 2024-09-08 03:13:38

什么给?有用。只需忽略它即可:

@SuppressWarnings("unchecked")

不,您不能参数化它。不过,我宁愿使用 List> 来代替。

List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();

要了解有关集合和映射的更多信息,请查看本教程

What gives? It works. Just ignore it:

@SuppressWarnings("unchecked")

No, you cannot parameterize it. I'd however rather use a List<Map<K, V>> instead.

List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();

To learn more about collections and maps, have a look at this tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文