Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Thread Tools Search this Thread
Old 04-03-2009
Join Date: Nov 2008
Posts: 85
Creating a Vector Object in Java

hi there

I am struggling through a problem in java. Can anyone help me in searchinh the way of creating a vector object in java. As i have seacrhed a lot at google but none of the results were worth. Please help me in this problem

Your views will be appreciated
Reply With Quote
Old 04-03-2009
Join Date: May 2008
Posts: 2,293
Re: Creating a Vector Object in Java

You must import either import java.util.Vector; or import java.util.*;. Vectors are implemented with an array, and when that array is full and an additional element is added, a new array must be allocated. Because it takes time to create a bigger array and copy the elements from the old array to the new array, it is a little faster to create a Vector with a size that it will commonly be when full. Of course, if you knew the final size, you could simply use an array. However, for non-critical sections of code programmers typically don't specify an initial size.

Create a Vector with default initial size
Vector v = new Vector();
Create a Vector with an initial size
Vector v = new Vector(300);

To Add elements to the end of a Vector

v.add(s); // adds s to the end of the Vector v
To get the elements from a Vector (ListIterator)

You can use a for loop to get all the elements from a Vector, but another very common way to go over all elements in a Vector is to use a ListIterator. The advantage of an iterator is that it it can be used with other data structures, so that if you later change to using a linked list for example, you won't have to change your code. Here is an example of using an iterator to print all elements (Strings) in a vector. The two most useful methods are hasNext(), which returns true if there are more elements, and next(), which returns the next element.

ListIterator iter = v.listIterator();
while (iter.hasNext()) {
Reply With Quote
Old 04-03-2009
Join Date: Oct 2005
Posts: 2,389
Re: Creating a Vector Object in Java

When you create a Vector, you can assign it to a List (a Collections interface). This will guarantee that only the List methods are called.

Vector v1 = new Vector(); // allows old or new methods.
List v2 = new Vector(); // allows only the new (List) methods.
Reply With Quote
Old 04-03-2009
Join Date: May 2008
Posts: 2,383
Re: Creating a Vector Object in Java

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

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.

As of the Java 2 platform v1.2, this class has been retrofitted to implement List, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Vector is synchronized.

The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Vector's elements method are not fail-fast.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
Reply With Quote
Old 04-03-2009
Join Date: Feb 2008
Posts: 1,848
Re: Creating a Vector Object in Java

public Vector(int initialCapacity,
int capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment.

initialCapacity - the initial capacity of the vector.
capacityIncrement - the amount by which the capacity is increased when the vector overflows.
Reply With Quote

  TechArena Community > Software > Software Development
Tags: , , , ,

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads for: "Creating a Vector Object in Java"
Thread Thread Starter Forum Replies Last Post
Creating a vector type of a class Xmen Software Development 5 24-02-2010 03:38 AM
Creating object and server prices in JAVA KALANI84 Software Development 5 24-11-2009 05:22 AM
How big is an Object in Java? Why Java neglects sizeof? KALINDA Software Development 4 10-11-2009 03:19 AM
Object test = new Object() <-- Java, best way in C++ ADISH Software Development 3 25-10-2008 02:32 PM

All times are GMT +5.5. The time now is 09:14 PM.