• Post Reply Bookmark Topic Watch Topic
  • New Topic

Varargs and List  RSS feed

 
Jerome Vatrinet
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have to call a java method using only varags in its prototype :



that is equivalent to :


My question is simple :
I only own a List<Object>
How can I set each element of _instanceMethod parameter from my List ??

If i decided to iterate through my list such as :



It will be wrong, it's obvious.

How can I correctly "populate" the _instanceMethod varargs signaturee by the n elements of my list ?

Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the java.util.List has a method to express itself as an array?

(Hint: it does.)
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerome Vatrinet wrote:

that is equivalent to :

No. It does not produce a method with unknown number of arguments. It is just a syntactic sugar. It is an equivalent to:
So you need to pass an array. If you already have List<Object> just use its toArray() method and call
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!