why can't I pass a squiggly brace expression into a method call?

K

Kevin Simonson

I have written a piece of code where it would be really handy if I
could pass an array defined as a series of values, separated by com-
mas, and enclosed by squiggly braces, into a method call. Notice that
in class <Hmm1> below I assign to variable <adg> an array of three
elements. The compiler lets me do that without complaining, and my
method <printArray> prints the contents of the three <Hmm1> objects
just fine.

But in class <Hmm2> when I try to pass a squiggly-brace-delimited
array directly into <printArray> the compiler takes a look at the left
squiggly brace and says, "[33:1] illegal start of expression".

I guess I _could_ build the array first and assign it to a vari-
able, and then pass the variable into <printArray>, but I was just
wondering if anybody knew a more direct way of doing this. Any ideas,
anyone?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

----------------------------------------------------------------------

public class Hmm1
{
int uvw;
int xyz;

public Hmm1 ( int uw
, int xz)
{
uvw = uw;
xyz = xz;
}

private static void printArray ( Hmm1[] arr)
{
int count;

for (count = 0; count < arr.length; count++)
{ System.out.println
( "arr[ " + count + "].uvw == " + arr[ count].uvw + " and arr[ "
+ count + "].xyz == " + arr[ count].xyz + '.');
}
}

public static void main ( String[] arguments)
{
Hmm1 abc = new Hmm1( 314, 159);
Hmm1 def = new Hmm1( 265, 358);
Hmm1 ghi = new Hmm1( 979, 323);
Hmm1[] adg = { abc, def, ghi };

printArray( adg);
}
}

----------------------------------------------------------------------

public class Hmm2
{
int uvw;
int xyz;

public Hmm2 ( int uw
, int xz)
{
uvw = uw;
xyz = xz;
}

private static void printArray ( Hmm2[] arr)
{
int count;

for (count = 0; count < arr.length; count++)
{ System.out.println
( "arr[ " + count + "].uvw == " + arr[ count].uvw + " and arr[ "
+ count + "].xyz == " + arr[ count].xyz + '.');
}
}

public static void main ( String[] arguments)
{
Hmm2 abc = new Hmm2( 314, 159);
Hmm2 def = new Hmm2( 265, 358);
Hmm2 ghi = new Hmm2( 979, 323);
Hmm2[] adg = { abc, def, ghi };

printArray( adg);
printArray
({ new Hmm2( 846, 264), new Hmm2( 338, 327), new Hmm2( 950, 288)
, new Hmm2( 950, 288), new Hmm2( 419, 716), new Hmm2( 939, 937) });
}
}
 
K

Kevin Simonson

=Change the line:
=
=printArray ({ new Hmm2( 846, 264), ... } );
=
=To:
=
=printArray (new Hmm2[] { new Hmm2( 846, 264), ... } );

Christian, thanks! That solved the problem nicely.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
M

Michael Borgwardt

Kevin said:
I have written a piece of code where it would be really handy if I
could pass an array defined as a series of values, separated by com-
mas, and enclosed by squiggly braces, into a method call.

BTW, the technical expression for that is "array literal".
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top