Varargs

C

Chris Uppal

Just for academic interest (so far...):

Given this rather silly example:

void dumpEmAll(Object... all)
{
for (Object each : all)
System.out.println(each);
}

and given an array.

Object oneArray[] = //...

and a call similar to:

dumpEmAll(oneArray);

how do I make Java treat oneArray as the single parameter to dumpEmAll(), so
that the toString() method of Object[] will be called just once, rather than
having dumpEmAll() iterating over the elements of the array ?

The only ways I can think of are to mess with the -source flag to javac, or to
wrap oneArray in /another/ array. Both of which seem kinda clunky to me...

-- chris
 
A

Andrea Desole

Chris said:
The only ways I can think of are to mess with the -source flag to javac, or to
wrap oneArray in /another/ array. Both of which seem kinda clunky to me...
I would like to know how you can use javac for that. Personally, I don't
see any other solution besides wrapping.
 
C

Chris Uppal

Andrea said:
The only ways I can think of are to mess with the -source flag to
javac, or [...]
I would like to know how you can use javac for that [...].

Actually, now you mention it, so would I ;-) My mistake. I was thinking
about it the wrong way around...

(<mutters>Must remember to face /East/ when posting to Usenet in
future...</mutters>)

-- chris
 
J

John C. Bollinger

Chris said:
Just for academic interest (so far...):

Given this rather silly example:

void dumpEmAll(Object... all)
{
for (Object each : all)
System.out.println(each);
}

and given an array.

Object oneArray[] = //...

and a call similar to:

dumpEmAll(oneArray);

how do I make Java treat oneArray as the single parameter to dumpEmAll(), so
that the toString() method of Object[] will be called just once, rather than
having dumpEmAll() iterating over the elements of the array ?

The only ways I can think of are to mess with the -source flag to javac, or to
wrap oneArray in /another/ array. Both of which seem kinda clunky to me...


Have you already tried inserting a cast to Object? I don't really know
whether that would work or not. The varargs docs do comment that the
feature is a bit quirky (my words). You could also put the array inside
of some wrapper object other than an array; if this were something you
needed to do frequently then you could write a wrapper class that
contained an Object and delegated toString() invocations to it. That
seems a little less clunky to me, but still not great.


John Bollinger
(e-mail address removed)
 
C

Chris Uppal

John said:
how do I make Java treat oneArray as the single parameter to
dumpEmAll()
[...]
Have you already tried inserting a cast to Object?

Great idea ! Thank you.

And it's a nice dramatic example of how an upcast can change Java semantics,
too.

I don't really know
whether that would work or not.

No need to test it, it /has/ to work... (I did test it anyway, though ;-)

In retrospect, I suppose it should have been obvious. Maybe in years to come I
shall claim that it always was obvious...

-- chris
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top