1

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 : Java usage of HashMap or Map with multidimensional arrays

Java HashMap associative multi dimensional array can not create or add elements

How to implement a Map with multiple keys?

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.

Community
  • 1
  • 1
user3064209
  • 147
  • 4
  • 14

1 Answers1

0

Sounds like your requirement involves mixing model and view, in that your view includes x,y coordinates, and you have data associated with each x,y coordinate. While I generally try not to mix model and view, your requirement has me thinking of a writeableraster. A writeableraster is a 1d array of pixels, but it starts at the upper left hand corner of the window and iterates rows and columns until it gets to the lower right hand corner. I think it goes column by column, but it might go row by row. You calculate the row and column by reconciling the writeable raster's index with the width/height of the window.

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.

CodeMed
  • 9,527
  • 70
  • 212
  • 364