File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Multidimensional Array Function Arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multidimensional Array Function Arguments" Watch "Multidimensional Array Function Arguments" New topic

Multidimensional Array Function Arguments

Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
I started with the following function declaration:

whereupon I get this error:

./src/infoIsland/ ']' expected
private String btnCell(String[][] btns[3][])

When I change to this:

This function, calling another function declared thusly:

gets this error:

./src/infoIsland/ 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?


-------<br />Thanks & regards,<br />anw
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
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

Joined: Sep 04, 2006
Posts: 136
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).


Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
Nope, addButtons(String[3][] btns)

gave the follow error:

./src/infoIsland/ ']' 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?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24166

Not in a method declaration, no. The type of an array variable includes the number of dimensions, but not the length along any dimension.

[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: Multidimensional Array Function Arguments
Similar Threads
Return item in array
Doubt in Array Declaration
Calculator clear button not working...
Common data for one (multi-instatianted) class
Passing array in an EL taglib function