How to restrict multi-dimensional array?

O

opalpa

Hi, I'd like to make a routine that takes and array of arrays where the
second dimension is always two. I'm suprised that code like this
works:

package experiment;
public class MultiDimensionalArrays {
__private MultiDimensionalArrays() {}
____static public void main(String args[]) {
____String s[][] = new String[4][2];
____s[1][1] = "s"; // <-- new not needed for every inner array!
____System.out.println("string s? " + s[1][1]);
__}
}

I see that this code does not work:

package experiment;
public class MultiDimensionalArrays {
__private MultiDimensionalArrays() {}
__static void takeAr(String a[4][2]) {} // <-- not okay
__static public void main(String args[]) {
____String s[][] = new String[4][2];
____s[1][1] = "s"; // <-- new not needed for every inner array!
____System.out.println("string s? " + s[1][1]);
__}
}

How do you think about this?
 
R

Ryan Stewart

Hi, I'd like to make a routine that takes and array of arrays where the
second dimension is always two. I'm suprised that code like this
works:

package experiment;
public class MultiDimensionalArrays {
__private MultiDimensionalArrays() {}
____static public void main(String args[]) {
____String s[][] = new String[4][2];
____s[1][1] = "s"; // <-- new not needed for every inner array!
____System.out.println("string s? " + s[1][1]);
__}
}

I see that this code does not work:

package experiment;
public class MultiDimensionalArrays {
__private MultiDimensionalArrays() {}
__static void takeAr(String a[4][2]) {} // <-- not okay
__static public void main(String args[]) {
____String s[][] = new String[4][2];
____s[1][1] = "s"; // <-- new not needed for every inner array!
____System.out.println("string s? " + s[1][1]);
__}
}

How do you think about this?
First, I think that's an interesting way of dealing with Google's failure to
keep indentation. Second, the standard and sensible way of declaring arrays is:
String[][] s;

Finally, you don't specify a method to take a particular size of array. You just
want:
static void takeAr(String[][] a);

Consider if a method took only a specific size:
void foo(String[1] s) {}
void foo(String[2] s) {}
void foo(String[3] s) {}
void foo(String[4] s) {}
void foo(String[5] s) {}
void foo(String[6] s) {}
void foo(String[7] s) {}

All those would qualify as different methods. That would be a nightmare.
 
S

sujee

In the takeAr method declaration u hv sent a[4][2]. It is not a
argument(Variable).
What d u want send inside that method? string or 2 dim string array?
If u want to send string -->
__static void takeAr(String arg) {}

when calling
Eg : takeAr(a[4][2]);

If u want to send 2 dim string array
__static void takeAr(String a[][]) {}


when calling
takeAr(a);
If u want to take the dimention of the arrag, use length() method of
array.
 
K

Kevin McMurtrie

Hi, I'd like to make a routine that takes and array of arrays where the
second dimension is always two. I'm suprised that code like this
works:

package experiment;
public class MultiDimensionalArrays {
__private MultiDimensionalArrays() {}
____static public void main(String args[]) {
____String s[][] = new String[4][2];
____s[1][1] = "s"; // <-- new not needed for every inner array!
____System.out.println("string s? " + s[1][1]);
__}
}

I see that this code does not work:

package experiment;
public class MultiDimensionalArrays {
__private MultiDimensionalArrays() {}
__static void takeAr(String a[4][2]) {} // <-- not okay
__static public void main(String args[]) {
____String s[][] = new String[4][2];
____s[1][1] = "s"; // <-- new not needed for every inner array!
____System.out.println("string s? " + s[1][1]);
__}
}

How do you think about this?

It would be a nightmare to enforce because the compiler and run-time
would have to check against an limitless number of combinations.
Imagine how hard it would be to evaluate (x instanceof
String[7][35][6][3][7][9]). It would have to check 7 arrays to make
sure they're length 35, check 245 arrays to make sure they're length 6,
check 1470 arrays to make sure they're length 3, check 4410 arrays to
make sure they're length 7, and finally check 30870 arrays to make sure
they're size 9.

Define a class that enforces the storage.
 
T

Tor Iver Wilhelmsen

Hi, I'd like to make a routine that takes and array of arrays where the
second dimension is always two.

Java does not enforce such a thing. C# has "real" multidimensional
arrays by using e.g. int[2,2] instead of int[2][2].
 
J

John C. Bollinger

(e-mail address removed) wrote:

Surprisingly, none of the other responders seems yet to have hit on the
key point here, which is that array types include the "number of
dimensions" as part of the type, but not the sizes. Thus "String[][]"
is a valid type, but "String[4][]" and "String[4][2]" are not, even
though both of the latter can be used in conjunction with the "new"
operator to instantiate objects of type String[][].
 
O

opalpa

Ryan,

Your comment reminds of pascal's strings, these have length as part of
type and library routines are

written with strings of 255 bytes (if I recall accurately). I see the
nightmare.



Sujee,

I was seeing if I could specify the type of an array of arrays where
the second dimension has to be two.

Specificaton meaning that calling code which tries to pass a parameter
of anything else does not compile.



Kevin,

That's indeed expensive, prohibitively so.



Tor Iver,

Have lee-way with this C#:

void method(int a[4,2]) { ... }

void main() {
int b[4,3];
method(b); // <-- compile error?
int c[4][2];
method(b); // <-- compile error?
int d[][] = new int[4][];
d[0] = new int[3];
d[1] = new int[1];
d[2] = new int[4];
d[3] = new int[2];
method(d); // <-- compile error?
}



John,


Types is what I'm thinking of here too John. Does:

String a[][] = new String[4][2];

mean the same thing as:

String a[][] = new String[4];
for (int i=0; i<a.length; i++)
__a = new String[2];


It is pleasent to do so, .... but wait!

http://java.sun.com/docs/books/tutorial/java/data/multiarrays.html

The second example is out of date? Is it not standard? Can I rely on
it?

It comes from feeling lucky on query = multidimensional arrays java

Thank you!
 
R

Ryan Stewart

Types is what I'm thinking of here too John. Does:

String a[][] = new String[4][2];

mean the same thing as:

String a[][] = new String[4];
for (int i=0; i<a.length; i++)
__a = new String[2];

Almost. The first line needs to be:
String a[][] = new String[4][];

And again, it should really be "String[][] a" for consistency and plain old
common sense.
It is pleasent to do so, .... but wait!

http://java.sun.com/docs/books/tutorial/java/data/multiarrays.html

The second example is out of date? Is it not standard? Can I rely on
it?
Out of date? Not standard? What do you mean?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top