Home > Archive > Java Help > June 2005 > Vector question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| jrefactors@hotmail.com 2005-06-02, 8:58 pm |
| 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);
}
}
}
| |
|
| <jrefactors@hotmail.com> wrote in message
news:1117737389.740758.239280@z14g2000cwz.googlegroups.com...
> // 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".
| |
|
| On Thu, 2 Jun 2005 jrefactors@hotmail.com wrote:
> 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);
> }
> }
> }
--
Send e-mail to: darrell dot grainger at utoronto dot ca
| |
| blmblm@myrealbox.com 2005-06-06, 3:59 am |
| In article <Pine.GSO.4.58.0506021443410.23508@drj.pf>,
.. <darrell@does.want.spam.com> wrote:
>On Thu, 2 Jun 2005 jrefactors@hotmail.com wrote:
>
>
>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).
>
>--
>Send e-mail to: darrell dot grainger at utoronto dot ca
>
--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.
|
|
|
|
|