Vector question

J

jrefactors

In the following program that uses Vector. I don't understand
what's the differences among 1,2, and 3, because they produce
the same output. Even I expect (2) and (3) should produce
buffer overflow. any ideas??


import java.util.*;

public class VectorTest
{ public static void main(String[] args)
{
Vector v = new Vector(); //(1): Constructs an empty vector so that its
internal data array has size 10 and its standard capacity increment is
zero.
// Vector v = new Vector(3); //(2): Constructs an empty vector with the
specified initial capacity and with its capacity increment equal to
zero. I expect to have buffer overflow
// Vector v = new Vector(3,2); //(3):Constructs an empty vector with
the specified initial capacity and capacity increment I expect to have
buffer overflow

for (int i=0; i < 20; i++)
{
v.addElement("Jenny_" + i);
}

for (Iterator iter = v.iterator(); iter.hasNext();)
{ String data = (String)iter.next();
System.out.println(data);
}
}
}
 
S

Skip

// Vector v = new Vector(3); //(2): Constructs an empty vector with the
specified initial capacity and with its capacity increment equal to
zero. I expect to have buffer overflow

see that "initial capacity" ? it will grow if you keep adding elements.
that's why it's called "initial".
 
?

.

In the following program that uses Vector. I don't understand
what's the differences among 1,2, and 3, because they produce
the same output. Even I expect (2) and (3) should produce
buffer overflow. any ideas??

A vector is like an array EXCEPT it grows in size as needed. You should
see no obvious difference between Vector 1, 2 or 3. There might be some
performance issues. The first Vector is the most efficient for this
program. The third Vector is the worst.
import java.util.*;

public class VectorTest
{ public static void main(String[] args)
{
Vector v = new Vector(); //(1): Constructs an empty vector so that its
internal data array has size 10 and its standard capacity increment is
zero.
// Vector v = new Vector(3); //(2): Constructs an empty vector with the
specified initial capacity and with its capacity increment equal to
zero. I expect to have buffer overflow
// Vector v = new Vector(3,2); //(3):Constructs an empty vector with
the specified initial capacity and capacity increment I expect to have
buffer overflow

for (int i=0; i < 20; i++)
{
v.addElement("Jenny_" + i);
}

for (Iterator iter = v.iterator(); iter.hasNext();)
{ String data = (String)iter.next();
System.out.println(data);
}
}
}
 
B

blmblm

A vector is like an array EXCEPT it grows in size as needed. You should
see no obvious difference between Vector 1, 2 or 3. There might be some
performance issues. The first Vector is the most efficient for this
program. The third Vector is the worst.

The OP didn't ask and so perhaps has read the documentation for Vector
more carefully, but:

The reason for this is that a capacity increment of zero doesn't mean
"this Vector can't grow" (which is what one might think) but "this
Vector grows by doubling in size". As you say, that's likely to be
more efficient.

And -- anyone else find the use of the term "buffer overflow" not quite
right in this context? According to the Wikipedia definition (and what
I usually think), a buffer overflow involves overflowing the intended
data area and (usually) messing up something else, which is supposed to
be impossible in Java (one should get some kind of exception instead).
import java.util.*;

public class VectorTest
{ public static void main(String[] args)
{
Vector v = new Vector(); //(1): Constructs an empty vector so that its
internal data array has size 10 and its standard capacity increment is
zero.
// Vector v = new Vector(3); //(2): Constructs an empty vector with the
specified initial capacity and with its capacity increment equal to
zero. I expect to have buffer overflow
// Vector v = new Vector(3,2); //(3):Constructs an empty vector with
the specified initial capacity and capacity increment I expect to have
buffer overflow

for (int i=0; i < 20; i++)
{
v.addElement("Jenny_" + i);
}

for (Iterator iter = v.iterator(); iter.hasNext();)
{ String data = (String)iter.next();
System.out.println(data);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top