• 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

Multidimensional Array Function Arguments

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic