Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Vector question
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);
}
}
}


Report this thread to moderator Post Follow-up to this message
Old Post
jrefactors@hotmail.com
06-03-05 01:58 AM


Re: Vector question
<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".



Report this thread to moderator Post Follow-up to this message
Old Post
Skip
06-03-05 01:58 AM


Re: Vector question
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


Report this thread to moderator Post Follow-up to this message
Old Post
.
06-03-05 01:59 AM


Re: Vector question
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.

Report this thread to moderator Post Follow-up to this message
Old Post
blmblm@myrealbox.com
06-06-05 08:59 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:42 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.