This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

finding vector's size  RSS feed

 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a vector with predefined size 4.
And now i would like to know how many objects are present in that vector??
When i am trying vector.size() in for loop its giving the error because of its predefined size.
Any one can help...
Thank you
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to iterate through the members of a Vector, use the Enumeration you get from Vector.elements()
 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i dont want to iterate. i just want to know the original size and i have to compare it with another variable....
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"rasagna" who was formerly "j@smin j@smin" - well, I suppose you're getting closer. But the new name doesn't work either. We need a first and last name. That implies a minimum of two. Thank you.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rasagna:
When i am trying vector.size() in for loop its giving the error because of its predefined size.
Well, next time it does that, have a look at the error message. The words in an error message are usually designed so that they describe the problem that is happening, and that is most likely the case with the error message you are getting there. If you don't understand the message, then post it here.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using the term "predefined size" which is not defined for Vector. Why not post a short example program demonstrating your problem. 5 or 10 lines should suffice.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would venture a guess that you are mistaking a Vector's size for it's capacity. I'm guessing you are creating it using new Vector(4) which returns a Vector with an initial capacity of 4. It's size is 0 until something is added. size() will tell you how many objects are present in the Vector and it has nothing to do with the initial capacity you set it to.

Edit: Oops I meant size() not getSize(). Fixed.
[ May 23, 2006: Message edited by: Ken Blair ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!