• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Vector capacity after cloning.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After cloning a vector of default capacity , how is the capacity of Vector u decided ?? Also capacity after adding 40 is still 4.



import java.util.*;
import java.util.Enumeration;

class st {

public static void main(String args[]) {

Vector v=new Vector();
Vector u=(Vector)v.clone();

System.out.println("v capacity "+v.capacity()); //prints 10
System.out.println("u capacity "+u.capacity()); //prints 0

u.add(10);
System.out.println("u capacity "+u.capacity()); //prints 1

u.add(20);
System.out.println("u capacity "+u.capacity()); //prints 2

u.add(30);
System.out.println("u capacity "+u.capacity()); //prints 4

u.add(40);
System.out.println("u capacity "+u.capacity()); //prints 4

u.add(50);
System.out.println("u capacity "+u.capacity()); //prints 8

}
}
 
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please indent your code and use the code button, and take note of what Rob told you. The documentation for Vector says the default it capacity 10, increment 0, but what you have there is initial capacity 0, increases by 1 or doubles whichever is the greater, when capacity is exceeded. It must be something to do with cloning; have you read what the clone() method in Vector does? You will find all the details in the src.zip file in your Java™ installation folder. Go through your code with a pencil and paper and count how many elements there are in the Vector and what its capacity are at each stage.
Why are you using Vector and Enumeration in the first place?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

where can i found src.zip.
 
Saloon Keeper
Posts: 13891
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mahi, like Campbell said, it's in your installation folder.

The typical location a Windows system would be something like: C:\Program Files\Java\jdk1.6.0_20\src.zip
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You really need to update your JDK
 
Stephan van Hulst
Saloon Keeper
Posts: 13891
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pfff I'm only 4 releases behind
 
Campbell Ritchie
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Pfff I'm only 4 releases behind

You mean you have got round to using JDK1.2?
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic