Vectors in Java
A Vector is a container that expands in size as new data is added. The Vector container cannot hold primitive variables. In can only hold objects. Therefore, if you wish to include a primitive data type in a vector you must use it's wrapper class.
Table 1.1 Primitive Wrapper Classes
By using the new operator with any of the wrapper classes, you can add a primitive variable to the vector.
For example:
Making a VectorCode:Integer nPrime = new Integer(5); Boolean bPrime = new Boolean(false);
You must first import the java.util.Vector class.
Constructors
The Vector class contains four constructors:
If you use the first constructor, an empty vector is created with a default size of 10, and it's capacity increment is zero.Code:Vector(); Vector(Collection coData); Vector(int nSize); Vector(int nSize, int nCapacity);
The second Vector constructor takes a Collection and populates the vector with the elements in the collection.
The third Vector constructor takes a single integer that specifies the beginning size of the constructor.
The forth constructor takes a integer specifiy the beginning size of the constructor and a second integer that specifies the increment.
Adding to a Vector
First create a Vector object. We will populate this vector with information about 3 people. This will include the following data:
1. Name (String)
2. Age (Integer)
3. Gender (Boolean - true represents male and false represents female)
4. Middle Initial (Character)
The data we want to add to the vector is this:
Table 1.2 Example data
Since we can only have objects added to our vector, we need to create a String object, an Integer object, a Boolean object, and a Character object. I am going to pass the four objects to a method that illustrates how to return a Vector.
The .add() method of the vector adds the specified element to the next space available in the Vector. I pass to a method the Vector, and the four elements that I wish to add to the Vector. The method adds the four elements to the vector, and returns the modified Vector.Code:public class VectorTest { public static void main(String[] args) { Vector vPerson = new Vector(); // a vector that has a size of 10 String sName; Integer nAge; Boolean bGender; Character cInitial; sName = "James"; // notice that there isn't a new String("James") line here. This isn't required, as Java has created a short form for assigning Strings. However I could have done sName = new String("James"); nAge = new Integer(16); bGender = new Boolean(true); cInitial = new Character('G'); vPerson = addElements(vPerson, sName, nAge, bGender, cInitial); sName = "Harry"; nAge = new Integer(25000); bGender = new Boolean(false); cInitial = new Character('R'); vPerson = addElements(vPerson, sName, nAge, bGender, cInitial); sName = "Flipper"; nAge = new Integer(97400); bGender = new Boolean(true); cInitial = new Character('A'); vPerson = addElements(vPerson, sName, nAge, bGender, cInitial); } public static Vector addElements(Vector vPerson, String sName, Integer nAge, Boolean bGender, Character cInitial) { vPerson.add(sName); vPerson.add(nAge); vPerson.add(bGender); vPerson.add(cInitial); return vPerson; } }
Now to output the results, I create a List iterator and four temporary primitive variables to hold the results.
Like so:
The iterator returns an Object, which I then convert to a String and using the wrapper class converting into back into the primitive data type.Code:ListIterator iterator = vPerson.listIterator(); String sTempName; int nTempAge; boolean bTempGender; char cTempInitial; while (iterator.hasNext()) { sTempName = iterator.next().toString(); nTempAge = Integer.parseInt(iterator.next().toString()); bTempGender = (Boolean)iterator.next(); cTempInitial = (iterator.next().toString().charAt(0)); System.out.print(sTempName + " " + cTempInitial + " is " + nTempAge + " years old,"); if (bTempGender) { System.out.print(" and is a male."); } else { System.out.print(" and is a female."); } System.out.println(); }
This code produces this output:
I've just covered adding elements to the Vector but you can remove elements from the Vector.James G is 16 years old, and is a male.
Harry R is 25000 years old, and is a female.
Flipper A is 97400 years old, and is a male.
Please note, that this is just an example. In an ideal situation, you would create a Person class. In which you could add Person objects to the Vector.
Hope this was helpful!
Enjoy,
James
External resources:
Java: Vectors
Vector (Java 2 Platform SE v1.4.2)
Last edited by chili5; 02-16-2009 at 09:00 AM.
Very nice tutorial, chili5! +rep
Very interesting !
Nice. I've always found Java's insistence on wrapper classes to access primitives annoying, though.
lol that was a typo.
@winged - Do you know why sun decided we have to use wrappers and not primitives?I find wrappers annoying also.
Java views classes as being a different type of data than primitives. This is different from C++'s perspective that classes and primitives should be equivalent. I think it's about Java's desire to be pure OOP, but still having to support primitives.
Nice Tutorial , +rep .!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks