Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes Assigning values in constructors with varargs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning values in constructors with varargs " Watch "Assigning values in constructors with varargs " New topic
Author

Assigning values in constructors with varargs

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2766
    
    9

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.


K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14669
    
  25

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41631
    
  31
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
Sheriff

Joined: Oct 13, 2005
Posts: 41631
    
  31
Maybe better to say that varargs are only suitable for setting up arrays, not for individiual members of that array.
 
 
subject: Assigning values in constructors with varargs