View Single Post
  #26 (permalink)  
Old 01-23-2008, 10:52 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Vectors
Hello mcal.

Since you seem interested I will start with vectors. In the java.util package are many tools and data structures that one can use for a variety of problems. For most problems, I prefer to use vectors. To use vectors, make sure that you import the java.util.Vector class.

Okay, a vector is a dynamic data structure unlike arrays. That means that vectors will grow automatically as you need more space. When using vectors, it is good practice to use generics. To define a vector object that can contain a variety of different objects we divine, lets say data, like this:
Code:
Vector<Object> data = new Vector<Object>();
This will create a empty vector of Object types. At the moment, data has no elements. To add an element, we use the Vector.add() method like this:
Code:
data.add("There is no spoon"); data.add(4400);
Note that 4400 is a literal and a primitive type. Java will box this automatically, that is turn the int into an Integer. To get the size of a vector, we use the Vector.size() method. To access an element of the vector, we use the Vector.get() method. In the code below we will print all the objects:
Code:
for (int i = 0; i < data.size(); i++){ System.out.println((data.get(i)).toString()); }
Are you following mcal?
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote