• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning values in constructors with varargs

 
K. Tsang
Bartender
Posts: 3406
13
Android 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • 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
Sheriff
Posts: 48652
56
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic