aspose file tools*
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
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: 2596
    
    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/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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: 39869
    
  28
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: 39869
    
  28
Maybe better to say that varargs are only suitable for setting up arrays, not for individiual members of that array.
 
wood burning stoves
 
subject: Assigning values in constructors with varargs