• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Handling multi-dimensional arrays.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
IntelliJ IDE Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just do:

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
T Shaw
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic