Forum Stats

  • 3,826,612 Users
  • 2,260,677 Discussions
  • 7,897,031 Comments

Discussions

Prob with ensureCapacity( ) in the context of vectors

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Jan 12, 2016 9:17AM in New To Java

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

  • Unknown
    edited Jan 7, 2016 3:39PM
      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 a capacityIncrement. The capacity 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 of capacityIncrement. 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.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jan 10, 2016 4:31AM

    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.

  • Unknown
    edited Jan 10, 2016 12:44PM
    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.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jan 12, 2016 9:17AM

    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.

This discussion has been closed.