Skip to content
Advertisement

How can I accept an argument list in a constructor and add it to a collection in Java?

I’m trying to make a constructor that would accept any number of variables of class “Item” as a variable argument list and add them to appropriate collection. What would be the best way to go about it?

My code so far:

JavaScript

Advertisement

Answer

Item... args should be fine.. You can then just do this

JavaScript

… instead of the static items block.

See similar code run live at IdeOne.com.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement