• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multidimensional Array Function Arguments

 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started with the following function declaration:



whereupon I get this error:


./src/infoIsland/ReqTag.java:255: ']' expected
private String btnCell(String[][] btns[3][])
^


When I change to this:



This function, calling another function declared thusly:



gets this error:


./src/infoIsland/ReqTag.java:260: addButton(java.lang.String,java.lang.String,java.lang.String) in infoIsland.ReqTag cannot be applied to (java.lang.String[][],java.lang.String[][],java.lang.String[][])
rtn+= addButton(btns[0][i], btns[1][i], btns[2][i]);
^


The bottom line is I want to pass in an indeterminate number of three-string arrays, then pass them as normal strings to another function. The reference I have says I can declare this:



but neither



Anyway, how DO you declare a 3xn string array, or, alternatively, an nx3 string array as a function argument?

TIA
 
Joni Salonen
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coming from C/C++? In Java an array is an object that has a property called lenght; it is not a simple block of memory. Therefore the issue with passing arrays as arguments to methods and not knowing how many elements they contain doesn't exist in Java.

> The reference I have says I can declare this:

Sounds like your reference is not very reliable; what you quote is utterly false.

In Java the brackets that declare an array can go behind both the type and the varible, so "String[][] btns[][]" declares a four-dimensional array of Strings. I think you meant to write "String[][] btns" which would be only two-dimensional.

[ October 01, 2006: Message edited by: Joni Salonen ]
[ October 01, 2006: Message edited by: Joni Salonen ]
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, many, many years at C/C++, a short time with Java!

So (I'm fixing to go try this), I should say:

function(String[3][] btns).

Right?

TIA
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, addButtons(String[3][] btns)

gave the follow error:

./src/infoIsland/ReqTag.java:251: ']' expected
private String btnCell(String[3][] btns)
^


However,addButtons(String[][] btns)

does work! So there's no way to give it one dimension that I want to be fixed?

TIA
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in a method declaration, no. The type of an array variable includes the number of dimensions, but not the length along any dimension.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java method declarations cannot enforce the size of the array, only its number of dimensions. You must use run-time checks to ensure the array is of the right size in each dimension.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic