• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing an Array to a Varargs method...

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, I've read this, but I can't tell if I'm having the same problem, or the opposite problem.

Basically, I'm trying to call a method with a varargs argument, but the parameters I want to pass are already in an array. I want to represent each item in the array as a seperate parameter to the method call.
example:



Problem is, the "foo" method is seeing the incoming arguments as a single argument of type Integer[], when I want it to see them as seperate arguments of type Integer.

Is there a way to do this, without knowing how many parameters will be passed?
 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind. Figured it out.

For anyone who has this same problem: You're doing something wrong. One of the goals of varargs is specifically to support this kind of thing.

In my case, I was incorrectly converting an ArrayList to an Array (or something).
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Varargs always give problems with Object and subclasses. An Integer[] is both an Object[] and an Object. In these cases, it's always wise to cast to either Object[] (if you want the elements to be the actual varargs arguments) or to Object (if you want the array to be one single varargs argument).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic