This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Assigning values in constructors with varargs

 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, suppose I have the following:



My question is how do I assign the values in the Video class such that there will not be ArrayIndexOutOfBoundsException? Also how can I assign the correct values to the correct variables (eg "xxx" corresponds to videoXXX variable and not videoType variable for vid2)?

Thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The varargs variable 'data' behaves like an array inside the constructor. So you can use '.length' just like on any other array to see how many elements it contains.
 
Marshal
Posts: 65085
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by K. Tsang:
. . . Also how can I assign the correct values to the correct variables (eg "xxx" corresponds to videoXXX variable and not videoType variable for vid2)?

Thanks.

Using varargs, I think not at all. I think you have demonstrated how unsuitable varargs are for constructors.
 
Campbell Ritchie
Marshal
Posts: 65085
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe better to say that varargs are only suitable for setting up arrays, not for individiual members of that array.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!