有没有办法实现LinkedHashMap的数组?

发布于 2024-09-30 21:17:21 字数 561 浏览 7 评论 0原文

我正在尝试实现 LinkedHashMap 数组,但我不知道是否可能......

目前我的代码如下所示:

public class LHMap {

    public static void main(String[] args) {

     LinkedHashMap<String, Integer>[] map = null;

  for (int i = 0; i < 5; i++) {
         map[i] = new LinkedHashMap<String, Integer>();
  }

  map[0].put("a", 0);
  System.out.println(map[0].get("a"));

 }
}


System.out.println(extracted(map)[0].get("a"));
returns a "NullPointerException"...

您知道如何实施吗?

编辑:1.擦除提取(),2.表->数组

I am trying to implement an array of LinkedHashMap but I don't know if it's possible...

For the moment my code looks like as follows :

public class LHMap {

    public static void main(String[] args) {

     LinkedHashMap<String, Integer>[] map = null;

  for (int i = 0; i < 5; i++) {
         map[i] = new LinkedHashMap<String, Integer>();
  }

  map[0].put("a", 0);
  System.out.println(map[0].get("a"));

 }
}


System.out.println(extracted(map)[0].get("a"));


returns a "NullPointerException"...

Have you got any idea how to implement this?

EDIT : 1. erase extracted(), 2. table->array

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

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

发布评论

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

评论(2

甜味拾荒者 2024-10-07 21:17:21

我不知道您的 extracted() 方法想要做什么。您收到 NullPointerException 是因为您将 map 传递到 extracted 中,然后立即返回它。在您的示例中,您传入 null 然后返回它。您无法在空数组上找到第 ith 下标(或任何下标)。

List 可以代替 LinkedHashMap 数组吗?

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

for(int i = 0; i < 5; i++) {
   listOfMaps.add(new LinkedHashMap<String, Integer>());
}

listOfMaps.get(0).put("a", 0);
System.out.println(listOfMaps.get(0).get("a"));

编辑

顺便说一下,你不能用泛型实例化数组。如果您想要类型安全,我建议您使用该列表。否则你就不得不凑合:

LinkedHashMap<String, Integer>[] map = new LinkedHashMap[5];

但是,这会给你警告。

I don't know what your extracted() method is trying to do. You're getting a NullPointerException because you're passing map into extracted and then you're returning it immediately. In your example, you're passing in null and then returning it. You can't find the ith subscript (or any subscript) on a null array.

Instead of an array of LinkedHashMap, would a List work?

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

for(int i = 0; i < 5; i++) {
   listOfMaps.add(new LinkedHashMap<String, Integer>());
}

listOfMaps.get(0).put("a", 0);
System.out.println(listOfMaps.get(0).get("a"));

EDIT

You cannot instantiate an array with generics, by the way. I'd suggest going with the list if you want the type-safety. Otherwise you'd have to make do with:

LinkedHashMap<String, Integer>[] map = new LinkedHashMap[5];

This will give you warnings, however.

蒗幽 2024-10-07 21:17:21
map[i] = new LinkedHashMap<String, Integer>();

这将导致 NPE,因为您从不初始化映射,或者更确切地说,您显式地将其初始化为 NULL:

LinkedHashMap<String, Integer>[] map = null;

编辑:

您需要实例化数组,例如:

map = (LinkedHashMap<String, Integer>[]) new LinkedHashMap[5];

尽管您会收到该代码的“未经检查的转换”警告。另请检查以了解通用数组创建问题(感谢那些指出它的人出去)。

map[i] = new LinkedHashMap<String, Integer>();

That will cause an NPE because you never initialize map, or rather, you explicitly initialize it to NULL:

LinkedHashMap<String, Integer>[] map = null;

EDIT:

You need to instantiate the array, e.g.:

map = (LinkedHashMap<String, Integer>[]) new LinkedHashMap[5];

Although you'll get an "unchecked conversion" warning with that code. Also check this for the generic array creation issue (thanks for those who pointed it out).

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