Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Prob with ensureCapacity( ) in the context of vectors

Hi,
I have made program:
import java.awt.*;
import javax.swing.*;
import java.util.Vector;
class testVector{
public static void main(String[] args) {
Vector <Integer> v = new Vector<Integer>( ); //v1= [ ], size =0, capacity =10
for (int j=1; j<= 5; j++)
v.add(new Integer(j));
// print them all back again
for (int i = 0; i < v.size(); i++)
JOptionPane.showMessageDialog(null,v.get(i));
// same thing with for-each
for (int a : v)
JOptionPane.showMessageDialog(null, a);
Vector <Integer> v2 = new Vector<Integer>(3,4);//capacity =3, increment=4
for(int j=4; j<=8;++j)
v2.add(new Integer(j));
// print them all back again
for (int i = 0; i < v2.size(); i++)
JOptionPane.showMessageDialog(null,v2.get(i));
// same thing with for-each
for (int a : v2)
JOptionPane.showMessageDialog(null, a);
JOptionPane.showMessageDialog(null,"Cap="+v2.capacity());
v2.ensureCapacity(9);
JOptionPane.showMessageDialog(null,"Cap="+v2.capacity());
}
}
Its printinting capacity 11. I cant understand this.
for(int j=4; j<=8;++j)
v2.add(new Integer(j));
I understand that after this loop capacity becomes 7 because of the increment argument in the constructor. But when we execute ensureCapacity(9) why it changes the capacity. We are not doing any insertion in the vector.
However, if i write:
v2.ensureCapacity(50)
capacity changes to 50.
Somebody please guide me.
Zulfi.
Comments
-
Vector <Integer> v2 = new Vector<Integer>(3,4);//capacity =3, increment=4 Its printinting capacity 11. I cant understand this. for(int j=4; j<=8;++j) v2.add(new Integer(j)); I understand that after this loop capacity becomes 7 because of the increment argument in the constructor. But when we execute ensureCapacity(9) why it changes the capacity. We are not doing any insertion in the vector. However, if i write: v2.ensureCapacity(50) capacity changes to 50.
The MINIMUM the capacity will increase is 4 because you used 'increment=4' in the constructor,
Since the capacity was 7 it becomes 11; 7 + 4 = 11.
When you use 'ensureCapacity(50)' that value is greater than the default increment so it is used. Java does NOT try to get to 50 by extending 4 at a time; that would be ridiculous.
See the Javadocs for Vector
https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
Each vector tries to optimize storage management by maintaining a
capacity
and acapacityIncrement
. Thecapacity
is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size ofcapacityIncrement
. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.If you add items one at a time then Java will extend the capacity 4 elements at a time if it gets full.
But if you specify 'ensureCapacity' Java will extend the capacity by 4 or by the value you provide, whichever is larger.
-
Hi,
Thanks for reply but i cant get answer to my question.
I have reduced the program to:
import java.awt.*;
import javax.swing.*;
import java.util.Vector;
class testVector2{
public static void main(String[] args) {
Vector <Integer> v2 = new Vector<Integer>(3,4);//capacity =3, increment=4
for(int j=4; j<=8;++j)
v2.add(new Integer(j));
JOptionPane.showMessageDialog(null,"Cap="+v2.capacity());//prints capacity 7: right, no problem
v2.ensureCapacity(9);// prob with this statement
JOptionPane.showMessageDialog(null,"Cap="+v2.capacity());// prints capacity 11: Why not 9?
}
}
I have problem with the statement:
v2.ensureCapacity(9);
Before execution of this statement, capacity was 7. I understand this. But after execution of this statement capacity becomes 11. i cant understand this. Why Its not executing the statement:
v2.ensureCapacity(9);
However if i change this statement to:
v2.ensureCapacity(50);
it would make the capacity 50. Why is
v2.ensureCapacity(9);
not working?? Somebody please guide me.
Zulfi.
-
Thanks for reply but i cant get answer to my question.
Yes you can, and did.
I gave you the answer in my first reply. Read the ENTIRE reply again. Pay attention to this part:
The MINIMUM the capacity will increase is 4 because you used 'increment=4' in the constructor, Since the capacity was 7 it becomes 11; 7 + 4 = 11. When you use 'ensureCapacity(50)' that value is greater than the default increment so it is used. Java does NOT try to get to 50 by extending 4 at a time; that would be ridiculous.
Tell us specifically which of those three statements you don't understand.
Vector <Integer> v2 = new Vector<Integer>(3,4);//capacity =3, increment=4
That says make the MINIMUM INCREMENT = 4. That is whenever you need to increment add AT LEAST 4 to the capacity.
v2.ensureCapacity(9);// prob with this statement JOptionPane.showMessageDialog(null,"Cap="+v2.capacity());// prints capacity 11: Why not 9?
Why not 9? Because the old capacity was 7 and the MINIMUM INCREMENT is 4. Focus on the word MINIMUM.
Vector <Integer> v2 = new Vector<Integer>(3,4);//capacity =3, increment=4
What is 7 + 4? Please post your answer.
Before execution of this statement, capacity was 7. I understand this. But after execution of this statement capacity becomes 11. i cant understand this.
You can't understand that 7 + 4 = 11? Not sure what to tell you. Let me try again:
Why not 9? Because the old capacity was 7 and the MINIMUM INCREMENT is 4. Focus on the word MINIMUM.
Did you see the word MINIMUM that time. If not read it a few more times.
Why Its not executing the statement: v2.ensureCapacity(9);
IT DID EXECUTE THAT STATEMENT.
The capacity is now 11. That ENSURES ('ensureCapacity') that you can store at least 9 without extending any more.
11 is greater than 9. So if it can hold 11 it can hold 9.
The term 'ensureCapacity' means: make sure you can hold AT LEAST the number I tell you.
You told it to MAKE SURE it can hold 9. The MINIMUM INCREMENT is 4. The current capacity is 7.
Add 4 to 7 and you get 11. It is that simple.
The term 'ensureCapacity' does NOT mean set the capacity to the value I give you give it. That would be 'setCapacity'; there IS NO SUCH METHOD.
1. Your wife has 7 eggs in the refrigerator.
2. She needs 9 eggs to make three-egg omelets for you, her and your ugly rug rat.
3. The INCREMENT value for eggs is 12 - one dozen
4. How many eggs MUST YOU buy at the market if you want to buy eggs? (HINT: what is the value for INCREMENT?)
5. How many total eggs will you have after you return home from the market? (HINT: add 9 and 12)
It is REALLY SIMPLE.
There IS NO method to SET the capacity to any value you feel like.
-
Hi,
I tried to focus on MINIMUM but i think word MAXIMUM is more appropriate here.
Now i would tell you what i understand.
At:
v2.ensureCapacity(9);
capacity is 7, so at this statement it would check whether 7+4> 9. If yes then capacity would be set to 11
At:
v2.ensureCapacity(50);
capacity is 7, so at this statement it would check whether 7+4> 50 which is not true then capacity would be set to 50.
Zulfi.