File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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: 1966
    
    7

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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36513
    
  16
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: 36513
    
  16
Maybe better to say that varargs are only suitable for setting up arrays, not for individiual members of that array.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning values in constructors with varargs
 
Similar Threads
Using Abstract class/methods (cannot find symbol)
Servlet request
Problem with passing variables between methods and classes (I think)
inner class question...
Using String