aspose file tools*
The moose likes Beginning Java and the fly likes Handling multi-dimensional arrays. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Handling multi-dimensional arrays." Watch "Handling multi-dimensional arrays." New topic
Author

Handling multi-dimensional arrays.

T Shaw
Greenhorn

Joined: Jul 29, 2012
Posts: 27
I need to create a 16 by 16 by X array to pass as an argument to a method wherein X changes depending on factors to be determined by the method. I'm quite new to Java, and the only programming experience I really ever had before this was a couple of years of playing with BASIC growing up, so I'm a little shaky on how to go about this. I figured, perhaps this would work:



And, well, this apparently isn't the right way to do it, or so my compiler said. I'm mostly teaching myself Java starting with Oracle's Java tutorials, but on the subject of arrays they leave me still feeling a little foggy.

Ultimately I want to deploy this to a recursive function that will fill in an individual array for a great deal of the X and Y values in the first two dimensions of the array, so if anyone can point me to the right syntax for passing and returning arrays from methods, I've not found anything enlightening on that subject either. Thanks.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

Almost right though. Change the last line to:


However, that only sets the 0,0 element of the array. All the others will still be null.
LQ McDonald III
Greenhorn

Joined: Aug 01, 2012
Posts: 10

Why not just do:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

Technically, what you are asking is impossible in java. The language does not support multi-dimensional arrays.

However, you can have a one-dimensional array that hold arrays.

You can even have a one-dimensional array that holds...arrays of arrays.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

T Shaw wrote:so I'm a little shaky on how to go about this.

I think Lq mcDonald covered your basic mistake; but what I'm more worried about is:
Ultimately I want to deploy this to a recursive function that will fill in an individual array for a great deal of the X and Y values in the first two dimensions of the array...
Recursion? Why?

Tip: Use recursion only if you have to; and if the first two dimensions are 16 x 16 (or hell, 1024 x 1024) you probably don't need it.

Recursion (like reflection, which will probably be the next thing you run into that "looks nice on paper") is tough; particularly for those of us not well-versed in Maths and 'proof by induction'. If you like it, and still want to go with it, document it well for those of us that don't (and there are plenty other than me ).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
T Shaw
Greenhorn

Joined: Jul 29, 2012
Posts: 27
Lq McDonald Iii wrote:Why not just do:


because i will vary in length. I need to not have a fixed length third dimension.
T Shaw
Greenhorn

Joined: Jul 29, 2012
Posts: 27
Winston Gutkowski wrote:
T Shaw wrote:so I'm a little shaky on how to go about this.

I think Lq mcDonald covered your basic mistake; but what I'm more worried about is:
Ultimately I want to deploy this to a recursive function that will fill in an individual array for a great deal of the X and Y values in the first two dimensions of the array...
Recursion? Why?

Tip: Use recursion only if you have to; and if the first two dimensions are 16 x 16 (or hell, 1024 x 1024) you probably don't need it.

Recursion (like reflection, which will probably be the next thing you run into that "looks nice on paper") is tough; particularly for those of us not well-versed in Maths and 'proof by induction'. If you like it, and still want to go with it, document it well for those of us that don't (and there are plenty other than me ).

Winston


The function I'm trying to write is a pathfinding function. Given an object is initially at (X,Y), I need to know the most expedient combination of x+1, x-1, y+1, and y-1 moves to get to (x,y). The only way I can think of doing that very well is to write something that will flow something like this:
pathfindingfunction {
save information about path to get to current x,y in array at [x][y]
for (int i = 0; i<4; i++) {
get new xy values from function that turns i=0,1,2, or 3 into move left, right, up, or down
check if new xy values are a legitimate place to stand
check if a path has already been made to new xy that leaves object with more movement left than the move presently being considered.
if this is the best way way yet to get to the new xy coordinates, pass the new coordinates to pathfindingfunction.
}
}

of course my preliminary sketch up of what's going to need to be in the function for it to work is much more complicated. It passes three arrays and three integers to do all that. What's fundamental for my purposes is that all those values except this final array I'm defining get thrown away once the process comes to the end of a given branch, at which point I need the older values as they existed at the time of the branch. Recursivity seemed the easiest way to go to appraise all these possible branches.

My father, who works mainly in C, told me that as long as I was working in an object-oriented language, I was going about the data handling all wrong and should make a 2x array of objects instead.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

T Shaw wrote:The function I'm trying to write is a pathfinding function. Given an object is initially at (X,Y), I need to know the most expedient combination of x+1, x-1, y+1, and y-1 moves to get to (x,y).

Oh, OK. Well, forget my previous rant then - but maybe not entirely.

There are several "best path" algorithms around, but I suspect that the "best" one for you will depend entirely on your rules.
It's true that path searching can be made easier by recursion, but only if:
(a) the logic for each branch is basically the same.
(b) each branch offers a distinct and correct choice from at least 2 alternatives (ie, you are dealing with at least a binary chop).

If not, your recursion is likely to flounder (and it's also not likely to be optimal).

Winston
T Shaw
Greenhorn

Joined: Jul 29, 2012
Posts: 27
I may look more into how to optimize pathfinding later, but as for now, this is mostly a matter of me practicing what little I know to get a better handle on the language in general. I find "hello world" programs to be inane and generally try to apply the concepts I'm learning toward something conceivably practical. I find it's better practice for me.

Now that I've been told to do this with objects, I created a class defining an individual object in my array:


and a class to create the array

which I invoke in main like this


but when I try to interact with the data like this:


The compiler says: "Exception in thread "main" java.lang.NullPointerException
at initalize.battle.InitalizeBattle.main(InitalizeBattle.java:44)"
which the internet says means the objects I'm trying to reference aren't there. I assume I blew the syntax again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Fred beat me to it, saying there is no such thing as a 16 × 16 array, only a 16‑member array of 16‑member arrays. The difference appears pedantic, but the Java approach allows much more flexibility than an m × n array.

And LQ McDonald III welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling multi-dimensional arrays.