Array declaration syntax question

B

barry

Hi - another silly Java syntax question. I have an 2D array, one
dimension I won't know the size of until runtime, the other dimension
is always 8. I have tried the following declaration

private short[8][] myArray=null;

but the compiler laughs at me. After much trying, I have to ask the NG
- what is the magic syntax?

Thank you
Barry
 
M

Michael Borgwardt

barry said:
Hi - another silly Java syntax question. I have an 2D array, one
dimension I won't know the size of until runtime, the other dimension
is always 8. I have tried the following declaration

private short[8][] myArray=null;

but the compiler laughs at me. After much trying, I have to ask the NG
- what is the magic syntax?

The size of a dimension is a property of an actual object, not of its tpye,
so you don't have to declare it as part of the type. Furthermore,
multidimensional arrays in Java are in fact arrays of arrays, so it looks
like this:

private short[][] myArray = new short[8][];
myArray[0] = new short[x];
 
G

Gordon Beaton

Hi - another silly Java syntax question. I have an 2D array, one
dimension I won't know the size of until runtime, the other dimension
is always 8. I have tried the following declaration

private short[8][] myArray=null;

but the compiler laughs at me. After much trying, I have to ask the NG
- what is the magic syntax?

This isn't C. You specify the dimensions when you initialize the
array, not as part of the type declaration.

private short[][] myArray;

int n = foo();
myArray = new short[8][n];

/gordon
 
S

Sudsy

barry said:
Hi - another silly Java syntax question. I have an 2D array, one
dimension I won't know the size of until runtime, the other dimension
is always 8. I have tried the following declaration

private short[8][] myArray=null;

but the compiler laughs at me. After much trying, I have to ask the NG
- what is the magic syntax?

Thank you
Barry

Here's an example:

public class x {

private short[][] myArray = new short[8][];

public x() {
for( int i = 0; i < myArray.length; i++ )
myArray = new short[i + 1];
}

...

}

In this case I just instantitate an array of i + 1 elements
and assign them to myArray such that I have the following
array elements defined:

myArray[0][0]
myArray[1][0]
myArray[1][1]
myArray[2][0]
myArray[2][1]
myArray[2][2]
....up to...
myArray[7][7]
 
D

Darryl L. Pierce

barry said:
Hi - another silly Java syntax question. I have an 2D array, one
dimension I won't know the size of until runtime, the other dimension
is always 8. I have tried the following declaration

private short[8][] myArray=null;

but the compiler laughs at me. After much trying, I have to ask the NG
- what is the magic syntax?

It's a multi-dimensional array, so the type is:

short[][]

The type will have no reference to the number of elements. That is defined
when the actual array is allocated.
 
T

Thomas G. Marshall

Michael Borgwardt said:
barry said:
Hi - another silly Java syntax question. I have an 2D array, one
dimension I won't know the size of until runtime, the other dimension
is always 8. I have tried the following declaration

private short[8][] myArray=null;

but the compiler laughs at me. After much trying, I have to ask the
NG - what is the magic syntax?

The size of a dimension is a property of an actual object, not of its
tpye, so you don't have to declare it as part of the type.
Furthermore, multidimensional arrays in Java are in fact arrays of
arrays, so it looks like this:

private short[][] myArray = new short[8][];
myArray[0] = new short[x];

Note that such and similar syntax is unnecessary. This works just fine:

int xSize = 7;
int a[][] = new int[5][xSize];

as verified by the following

public static void main(String[] args)
{
int xSize = 7;
int a[][] = new int[5][xSize];

for (int y=0; y<5; y++)
for (int x=0; x<xSize; x++)
a[y][x] = 9;

for (int y=0; y<5; y++)
{
for (int x=0; x<xSize; x++)
System.out.print(a[y][x]+" ");
System.out.println();
}
System.out.println();
}

Which produces:

9 9 9 9 9 9 9
9 9 9 9 9 9 9
9 9 9 9 9 9 9
9 9 9 9 9 9 9
9 9 9 9 9 9 9
 
M

Michael Borgwardt

Thomas said:
private short[][] myArray = new short[8][];
myArray[0] = new short[x];


Note that such and similar syntax is unnecessary. This works just fine:

int xSize = 7;
int a[][] = new int[5][xSize];

True, but does not allow you do have "jagged arrays" where the second dimension's
size varies within the array. Admittedly, it seems like barry didn't need that.
 
P

Phil...

My book says you can make jagged arrays
after they have been declaired.
I mean you can have abc[0][5]
abc[1][9]
etc.

Michael Borgwardt said:
Thomas said:
private short[][] myArray = new short[8][];
myArray[0] = new short[x];


Note that such and similar syntax is unnecessary. This works just fine:

int xSize = 7;
int a[][] = new int[5][xSize];

True, but does not allow you do have "jagged arrays" where the second dimension's
size varies within the array. Admittedly, it seems like barry didn't need that.
 
M

Michael Borgwardt

Phil... said:
My book says you can make jagged arrays
after they have been declaired.
I mean you can have abc[0][5]
abc[1][9]
etc.

That's exactly my point: you can do that, but only with the
method I described, not with the one Thomss described.
 
T

Thomas G. Marshall

Michael Borgwardt said:
Phil... said:
My book says you can make jagged arrays
after they have been declaired.
I mean you can have abc[0][5]
abc[1][9]
etc.

That's exactly my point: you can do that, but only with the
method I described, not with the one Thomss described.

You're wrong, you /can/ with the method I described.

It's useful, for example if what you want is a rectangle matrix in every
regard except for a few rows, which are of different sizes.

This is produced:

9 9 9 9 9 9 9
9 9 9 9 9 9 9
9 9 9 9 9 9 9 0 0 0 0 0 0 0 0
9 9 9 9 9 9 9
9 9 9 9 9 9 9

by the following code:

int xSize = 7;
int a[][] = new int[5][xSize];

a[2] = new int[15]; // <------NOTE------

for (int y=0; y<5; y++)
for (int x=0; x<xSize; x++)
a[y][x] = 9;

for (int y=0; y < a.length; y++)
{
for (int x=0; x < a[y].length; x++)
System.out.print(a[y][x]+" ");
System.out.println();
}
System.out.println();

Besides, my original point was that your solution was unnecessary given the
OP's question. Others had already given an adequate explanation of the
array of arrays topic, so I filled in what you had missed.
 
T

Thomas G. Marshall

Thomas G. Marshall <[email protected]>
coughed up the following:

....[self snippage]...

[Remember that this technique is in addition to the other
techniques explained, not in replacement of. In addition,
this technique is particularly well suited to the need for
box arrays with only a /few/ rows of odd lengths.]

It occurred to me that my example might not be enough for a newbie. I
specifically fill a box with 9 and then print the entirety of the array of
arrays, which has a leg of 15 elements, which are 0, to show the parts of
the original box array and the extra elements of row 2.

Perhaps it would have been clearer if I had filled the entire thing with
9's. This is an example where the fill loop is changed to:

for (int y=0; y < a.length; y++)
for (int x=0; x < a[y].length; x++)
a[y][x] = 9;

Producing:

9 9 9 9 9 9 9
9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9
9 9 9 9 9 9 9

....and this is all the code:

int xSize = 7;
int a[][] = new int[5][xSize];

a[2] = new int[15];

for (int y=0; y < a.length; y++)
for (int x=0; x < a[y].length; x++)
a[y][x] = 9;

for (int y=0; y < a.length; y++)
{
for (int x=0; x < a[y].length; x++)
System.out.print(a[y][x]+" ");
System.out.println();
}
System.out.println();
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top