Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

array instantiation  RSS feed

 
Kat Cobb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to set up a constructor to go with my array program. One of my data members is "private int[] scores". My constructor is supposed to take an array of integers as a parameter. It then calls a setScore method passing it the parameter array. First, is it possible to instantiate an array without setting its bounds? I need to be able to enter a different amount of numbers each time I run the program, but I can't get it to compile without putting a bound in...ie: private int[]scores = new int[11]. Second, when I call the method, I know I put "setScores()" but how would I pass it the parameter array? Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot instantiate an array without setting its size. However, you can declare an instance variable without instantiating it, and then instantiate it in the constructor when you know the size...

[ December 01, 2007: Message edited by: marc weber ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!