Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Array declaration syntax question

Reply
Thread Tools

Array declaration syntax question

 
 
barry
Guest
Posts: n/a
 
      10-15-2003
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
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-15-2003
barry wrote:
> 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];

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-15-2003
On 15 Oct 2003 02:40:35 -0700, barry wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      10-15-2003
barry wrote:
> 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[i] = new short[i + 1];
}

...

}

In this case I just instantitate an array of i + 1 elements
and assign them to myArray[i] 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]

 
Reply With Quote
 
Darryl L. Pierce
Guest
Posts: n/a
 
      10-15-2003
barry wrote:

> 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.

--
Darryl L. Pierce <(E-Mail Removed)>
Visit the Infobahn Offramp - <http://bellsouthpwp.net/m/c/mcpierce>
"What do you care what other people think, Mr. Feynman?"
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      10-15-2003
Michael Borgwardt <(E-Mail Removed)> coughed up the following:

> barry wrote:
>> 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



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-16-2003

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


see http://mindprod.com/jgloss/gotchas.html#ARRAY

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-16-2003
Thomas G. Marshall wrote:
>>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.

 
Reply With Quote
 
Phil...
Guest
Posts: n/a
 
      10-16-2003
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" <(E-Mail Removed)> wrote in message
news:bmlijr$obofa$(E-Mail Removed)-berlin.de...
> Thomas G. Marshall wrote:
> >>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.
>



 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-17-2003
Phil... wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a question about syntax of declaration with inheritance... SpreadTooThin C++ 1 03-29-2007 08:55 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
question on alternate function declaration syntax rcallan@seas.upenn.edu Java 1 12-14-2004 05:00 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments