I. Introduction
Vector of objects is a class in Java. Instead of arrays,
the class vector has many predefined methods that we can use.
Its number of elements vary dynamically, depending on adding
or retreiving elements from a vector. This class resides in
java.util class. We have then to import it. Now, let's
consider a vector of Circles. As we have alrady defined
this class in TestCircles class, we import the class Circles
in the same package and use it.We have the class Circles in the
same package.
Here, we built new Circles, by calling the constroctor
of the class Circles, as follows:
Circles Circle1 = new Circles (1.25) ,
Circle2 = new Circles (2) ,
Circle3 = new Circles (3.5) ,
Circle4 = new Circles (4) ;
First, we put them in a vector as follows:
Vector TheCircles = new Vector(); by building a vector with
the name TheCircles.
Next, we use the Java methods predefined in the class Vector:
TheCircles.addElment (Circle1);
TheCircles.addElment (Circle2);
TheCircles.size(); return 2.
TheCircles.setElementAt (Circle3,2); will memorize
Circle3 in the third index.
Circles Circlex = (Circles) TheCircles.elementAt(1);
will return the Circle Circlex which is in the second position.
If we do not cast as (Circles), the statement
will return an object from the Java class Object.
TheCircles.insertElementAt (Circle4, 2);
will insert the circle Circle4 in the third index of the vector.
final boolean removeElement (Object theObject);
remove the first element of the vector.
final void removeElementAt(int Rank);
remove the element at the index Rank.
final void removeAllElements();
will empty the vector.
final int indexOf (Object TheObject);
Return the index of the first occurence of the oject TheObject.
final int lastIndexOf (Object TheObject);
Return the index of the last occurence of the oject TheObject.
final boolean contains (Object TheObject);
Return true if the object is present in the Vector; false if not.
II. The related code
import java.util.*; //import java.util.Vector;
public class Vectors{
static void Display(Vector TheVector, String Words){
System.out.println(Words);
if (TheVector.size() == 0)System.out.println("The
vector is empty.");
else {
System.out.println ("\n\t The vector has " +
TheVector.size()+ " elements.");
for (int i =0; i < TheVector.size(); i++)
((Circles) TheVector.elementAt(i)).Display
("\n\t at the index " + i); // Display method of the class Cirles
System.out.println();
}
}
public static void main (String[] args){
// 1. Vector TheCircles = new Vector ();
// Construction of the vector TheCircles
Vector TheCircles = new Vector();
// Construction of the vector TheCircles
System.out.println("\n\t The number of elements
in the vector TheCircles at first, is: " + TheCircles.size());
Circles Circle1 = new Circles (1.25) ,
Circle2 = new Circles (2) ,
Circle3 = new Circles (3.5) ,
Circle4 = new Circles (4) ;
TheCircles.addElement(Circle1);
TheCircles.addElement(Circle2);
TheCircles.addElement(Circle3);
TheCircles.addElement(Circle4);
System.out.println("\n\t After adding 4 cercles, \n\t the
number of elements in the vector TheCircles is: " + TheCircles.size());
Circles Circle5 = new Circles (10);
TheCircles.insertElementAt(Circle5,4);
System.out.println("\n\t After inserting an element, \n\t
the number of elements in the vector TheCircles is: " + TheCircles.size());
//2. Circles Circlex = (Circles) TheCircles.elementAt(3);
// casing
Circles Circlex = TheCircles.elementAt(3);
System.out.println("\n\t The index of the elemnt Circle4
\n\t in the vector is: " + TheCircles.indexOf(Circle4));
Circles Circle6 = new Circles (100);
TheCircles.setElementAt(Circle6,0);
System.out.println("\n\t After setting Cercle6, \n\t
the number of elements in the vector TheCircles is: " + TheCircles.size());
TheCircles.removeElementAt(2);
System.out.println("\n\t After removing the third element,
\n\t the number of elemnts in the vector TheCircles is: " + TheCircles.size());
System.out.println();
TheCircles.removeElementAt(0);
Display (TheCircles, "\n\t After removing the first element,
we have:");// This Display
TheCircles.removeElementAt(0);
//3. ((Circles) TheCircles.firstElement()).Display (", first
element of the vector");//casting
( TheCircles.firstElement()).Display (", first element of the vector");
// Display of the Circles class
System.out.println();
TheCircles.removeAllElements();
System.out.println("\n\t After removing all the elements, \n\t
the number in the vector TheCircles is: " + TheCircles.size());
}
}//end of the class Vectors
III. Remarques
In the main method, the program runs ( no errors, and the
class is built) with the lines commented 1.,2.,3., but
it gives warinig regarding addElement and insertElement.
which shown by the command : javac -Xlint (with -Xlint option).
To eliminate the wornings add the brackets after Vector; that
is Vector (here the vector uses the class Circles),
and eliminate the casting.
IV. Execution
C:\Java>java Vectors
The number of elements in the vector TheCircles at first, is: 0
After adding 4 cercles,
the number of elements in the vector TheCircles is: 4
After inserting an element,
the number of elements in the vector TheCircles is: 5
The index of the elemnt Circle4
in the vector is: 3
After setting Cercle6,
the number of elements in the vector TheCircles is: 5
After removing the third element,
the number of elemnts in the vector TheCircles is: 4
After removing the first element, we have:
The vector has 3 elements.
The circle
at the index 0 has the
Surface = 12.566370614359172
The circle
at the index 1 has the
Surface = 50.26548245743669
The circle
at the index 2 has the
Surface = 314.1592653589793
The circle, first element of the vector has the
Surface = 50.26548245743669
After removing all the elements,
the number in the vector TheCircles is: 0
C:\Java>
|