Jump to content

  • Log In with Google      Sign In   
  • Create Account

[java] Java Map, HashMapint[], Integer


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 BASICFreak   Members   -  Reputation: 115

Like
0Likes
Like

Posted 10 August 2010 - 04:24 PM

here is part of my code:

Map<int[], Integer> MyMap = new HashMap<int[], Integer>();
Map<int[], Integer> VisibleMap = new HashMap<int[], Integer>();
Map<int[], Integer> MiniMap = new HashMap<int[], Integer>();
...
VisibleMap.clear();
for(int x = 0; x <= 60; x++)
{
for(int y = 0; y <= 40; y++)
{
int[] temp = {x, y};
VisibleMap.put(temp, 1);
}
}
MyMap.clear();
for(int x = 0; x <= 2048; x++)
{
for(int y = 0; y <= 2048; y++)
{
int[] temp = {x, y};
MyMap.put(temp, 0);
}
}
MiniMap.clear();
for(int x = 0; x <= 190; x++)
{
for(int y = 0; y <= 128; y++)
{
int[] temp = {x, y};
MiniMap.put(temp, 3);
}
}
...
for(int x = 0; x < 60; x++)
{
for(int y = 0; y < 40; y++)
{
int[] temp = {x, y};
int tile = (int)VisibleMap.get(temp);
renderTile(g, tile, x*10, y*10);
}
}
for(int x = 0; x < 190; x++)
{
for(int y = 0; y < 128; y++)
{
int[] temp = {x, y};
int tile = (int)MiniMap.get(temp);
renderMiniMapTile(g, tile, x*10, y*10);
}
}

I can't find a way to get the value of what was put into the map at [x, y]
The error(s):

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at HalfDoomed.game(HalfDoomed.java:312)
at HalfDoomed.paint(HalfDoomed.java:220)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


What am I doing wrong, how am I supposed to get a value when the key is a Int Array?
The only thing on google is a Int key and a String key no int array keys.
If you need the rest of my code just ask.

Sponsor:

#2 stanirya   Members   -  Reputation: 771

Like
0Likes
Like

Posted 10 August 2010 - 04:45 PM

Using arrays as keys in a map is generally not a good idea. Two different arrays with the same contents are two entirely different objects and won't result in the same hash value retrieved. If you describe what kind of data you are trying to store in more detail, we can come up with a better solution. If you absolutely want to use arrays as keys, you'll have to wrap them in another class and implement equals() and hashCode(), like described here for instance.

Looking at the above code, you are probably going to want to create a new class called Tile which stores the three integers for various map values and then simply use a two-dimensional array of Tiles.

As for the error, without knowing what line 312 is, I would guess that you're reading a value from one of those maps and trying to use it somewhere (renderTile() maybe?) but what you are getting from the map is obviously a null value.

#3 elFarto   Members   -  Reputation: 206

Like
0Likes
Like

Posted 10 August 2010 - 09:47 PM

The usual way to store tile data is something like:

int WIDTH = 2048
int HEIGHT = 2048;
int world[] = new int[WIDTH * HEIGHT];

int tile = world[y * WIDTH + x];

You're probably better off creating a Tile class instead of using an int.

Regards
elFarto

#4 Daniel L   Members   -  Reputation: 129

Like
0Likes
Like

Posted 15 August 2010 - 10:03 AM

Why are you using a HashMap as opposed to a plain 2D array? Does your map expand dynamically?

If you can't commit to a fixed size array, I'd write a Point class which holds two ints and implements equals and hashCode. Or if you don't mind limiting your coordinates to 16 bits each, you could do something like this:


Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int y = 0; y < H; ++y)
for (int x = 0; x < W; ++x)
map.put((y << 16) | x, 1);






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS