2-Dimensional iterable key for HashMap in Java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I ve got a rather large portion of 2-Dimensional data that I m trying to work with, every piece of data has an (x, y) coordinate, and then an array of data located at that coordinate.

What I m doing right now is just using a HashMap to cycle through it, with a method checking for the coordinate to know when to go to the next line (so if the data is [0-10] for x then it tests for 11 (and multiples) to calculate where to display a new line). This is rather cumbersome, and I m hoping there s an easier solution, but I haven t been able to find one on Google or SO yet that fits.

What I m trying to do is something to the following pseudocode :

   HashMap thisMap = (key = IntArray[][], value = IntArray[] )

Where I could set a particular 2-d Int array as the key for the entire thing, and then assign the values through a loop (and get the values just as easily) in an iterable way. The closest thing I can think of would be how PHP handles multi-dimensional arrays and foreach() where you can nest the foreach() loops, but I m not sure how this would work in Java.

The goal is eventually to be able to write two methods, one that will iterate along the X-axis (first digit), then proceed to the next row; and another that will take it as frames and iterate over the Y-axis. I m not wanting anyone to write these for me, or even the HashMap code, but I m hoping to be pointed in the correct direction on how to do this (if it is possible) with just the standard libraries available in Java SE and not overriding anything. If not, then hopefully directions in how to override safely.

  NOTE    

Another problem here is the set of data is quite large, and what I initially tried to do was to write my own Tuple class, but the first time I used it I ran out of heap memory, which is why I m trying to stick to a single 2-d primitive int array for this.

  EDIT   

I found these : http://stackoverflow.com/questions/17733746/java-usage-of-hashmap-or-map-with-multidimensional-arrays http://stackoverflow.com/questions/17733746/java-usage-of-hashmap-or-map-with-multidimensional-arrays

http://stackoverflow.com/questions/18326386/java-hashmap-associative-multi-dimensional-array-can-not-create-or-add-elements http://stackoverflow.com/questions/18326386/java-hashmap-associative-multi-dimensional-array-can-not-create-or-add-elements

http://stackoverflow.com/questions/822322/how-to-implement-a-map-with-multiple-keys?lq=1 http://stackoverflow.com/questions/822322/how-to-implement-a-map-with-multiple-keys?lq=1

however they do not seem to be attempting to achieve the same thing.

  EDIT 2   

another complication is that the keys in the Y-axis are not sequential, though they are ordered low to high.

Answers

http://docs.oracle.com/javase/7/docs/api/java/awt/image/WritableRaster.html http://docs.oracle.com/javase/7/docs/api/java/awt/image/WritableRaster.html

Writeable rasters are very fast because you are writing directly to the pixel array. You then send it to the gui through a bufferedimage and/or a graphics object attached to a JPanel or a Canvas.

As for the iteration structure, I use arraylists of arraylists to handle multivariable structures. The nice thing about arraylists is that they can be of variable length, unlike arrays whose lengths need to be defined when they are instantiated. But whether you use nested arraylists or nested arrays, you just nest as many layers as you need in order to handle your data structure.

If your code looks complex and cumbersome, it probably makes sense to break it down into lots of methods. When I have coded complicated algorithms in java, I have tried to follow a rule of keeping each method to less than ten lines of code. I map it all out in a flowchart with a box for each method and arrows pointing to the flow of control between methods as the algorithm loops.

Does this answer your question? It was kind of hard to understand the gist of your question because it seemed like you were still thinking it through while you were writing it.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/20591598/2-dimensional-iterable-key-for-hashmap-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils