0

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

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Michaël
  • 3,599
  • 7
  • 37
  • 61

2 Answers2

2

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.

Vivin Paliath
  • 87,975
  • 37
  • 202
  • 284
  • This solution works perfectly but isn't it "ArrayList> listOfMaps" instead of "List> listOfMaps"? – Michaël Nov 08 '10 at 23:18
  • Michaël, it is preferable to type an object with the interface instead of the concrete implementation (you're not tied to a specific implementation that way - gives you the advantage of loose coupling). `List` and `Map` are interfaces. Check out this link for more details: http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html Also found this SO question http://stackoverflow.com/questions/147468/why-should-the-interface-for-a-java-class-be-prefered – Vivin Paliath Nov 08 '10 at 23:20
  • Thank you for the links, the answer and comments. I understand, now, why I have to implement List but Eclipse returns an error with your implementation : "The type List is not generic; it cannot be parametrized with arguments >". – Michaël Nov 08 '10 at 23:28
  • That shouldn't happen; not if you're using `java.util.List`. I tested out the code and it compiles. – Vivin Paliath Nov 08 '10 at 23:31
  • My fault... I imported "java.awt.List" Thank you very much! – Michaël Nov 08 '10 at 23:33
0
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).

Community
  • 1
  • 1
vanza
  • 8,729
  • 1
  • 28
  • 33
  • I am using Eclipse and it returns an error when I instantiate the array : "Cannot create a generic array of LinkedHashMapy" – Michaël Nov 08 '10 at 23:13
  • -1 This code will not compile. You cannot instantiate an array with generics. You'll get a compile-time error `generic array creation`. – Vivin Paliath Nov 08 '10 at 23:14
  • How did this get voted up? This answer is **wrong**. You **cannot** instantiate an array with generics. – Vivin Paliath Nov 08 '10 at 23:16