what I cover:
1:Importing,
2:Declaration of vectors,
3:Adding elements to a vector,
4:Deleting elements in a vector,
5:Accessing an Element.
6:Discovering vector size,
7:Improving Efficiency.
What you need to know:
Scanner-input from keyboard
for loops - only two in final example
Small knowledge of class variables and primitive variables
1:Importing;
Ok! so the first thing you need is to import the vector utility from the standard library.
import java.util.Vector;
class example{
public static void main(String []args){
}//end of main
}//end of class
2:Declaration of Vectors.
import java.util.Vector;//imports vector utility
class example{
public static void main(String []args){
Vector<String> myVector=new Vector<String>(10,2);
}//end of main
}//end of class
This program declares a Vector of size 10 and its space will increase by 2 when more then 10 elements are added.
If it was declared like below
Vector<String> myVector=new Vector<String>();
Then the starting size would be 10 by default and it would double everytime it became full
The control over how fast the array grows is used to ensure the vector never gets needlessly big.
The "<String>" declares what variables the Vector can hold.
This can be replaced with other class type variables such as "<String>", "<Double>","Integer","Object" and so on... but no primitive types like "int" ;
3: Adding to a vector
Adding contents to a Vector can be done a number of ways.
The way I'll explain begins with declaring a vector of Strings.
import java.util.Vector; //imports vector utility
class example{
public static void main(String []args){
Vector<String> myVector=new Vector<String>(10,2); //declare vector
String sample="tester"; //test string declared
myVector.add(sample); //adds sample's value to the vector
System.out.println("Value is :"+myVector.get(0)); //Displays the value
}//end of main
}//end of class
This program puts a string "tester" into the vector then displays it back.
The function ".add()" adds the value placed within its brackets to the first empty element in the vector
The function ".get()" returns the value of an element at whatever index is placed inside its brackets.
My Program only had one element so I just accessed the first element.
You can also declare a vector of the other variable types mentioned above and place the same variable type as the vector in it.
The majority of the time I use vectors for storing objects that are defined by a seperate class program.
But I'm keeping this as simple as possible so maybe that's a tutorial for another day
4:Deleting Elements
To delete an element in an array we use the .remove() command
import java.util.Vector; //imports vector utility
class example{
public static void main(String []args){
Vector<String> myVector=new Vector<String>(10,2); //declare vector
String sample="tester"; //test string declared
myVector.add(sample); //adds sample's value to the vector
System.out.println("Value is :"+myVector.get(0)); //Displays the value
myVector.remove(0); //this removes the element at the specified position
}//end of main
}//end of class
This program above is program is fairly simple.
I place a string in the vector then remove it with the .remove() function at its position.
5:Accessing an Element.
I have already used and explained the .get() function in previous examples above.
So I'll show some other methods
First up its .elementAt()
import java.util.Vector; //imports vector utility
class example{
public static void main(String []args){
Vector<String> myVector=new Vector<String>(10,2); //declare vector
String sample="tester"; //test string declared
String holder; //Will hold the value of an element in the array
myVector.add(sample); //adds sample's value to the vector
holder=myVector.elementAt(0);
System.out.println("Value is :"+holder); //Displays the value
}//end of main
}//end of class
This program use's the elementAt() function to extract the value from the array into a normal variable and then display it
The next programme shows the .toString() function
import java.util.Vector; //imports vector utility
class example{
public static void main(String []args){
Vector<String> myVector=new Vector<String>(10,2); //declare vector
String e1="Element1Contense"; //this string will be later added to the vector at 0
String e2="Element2Contense"; //this string will be later added to the vector at 1
String holder; //this string will hold the values extracted for the vector
myVector.add(e1); //adds e1 to the vector
myVector.add(e2); //adds e2 to the vector
holder=myVector.toString(); //holder becomes equal to all the elements in the vector
System.out.println("Value is :"+holder); //Displays the value
}//end of main
}//end of class
The above program adds two elements to the vector then all the elements are extracted as one string which is displayed to the screen.
Useful for when your getting errors whilst using a vector.
6:Discovering vector size,
Discovering the size of a vector is a constant necessity as it increases and decrease in size.
This makes the demands on a loop in your program change as the program runs
Once again there's not much to this part. I'm going to make use of the .size() function
import java.util.Vector; //imports vector utility
class example{
public static void main(String []args){
Vector<String> myVector=new Vector<String>(10,2); //declare vector
String e1="Element1"; //this string will be later added to the vector at 0
String e2="Element2"; //this string will be later added to the vector at 1
int number; //this string will hold the values extracted for the vector
myVector.add(e1); //adds e1 to the vector
myVector.add(e2); //adds e2 to the vector
number=myVector.size(); //number becomes equal to the number of elements in the vector
System.out.println("The Number of elements are :"+number); //Displays the value
}//end of main
}//end of class
The Above program discovers the size of the vector. It would never really be used in the format that I have shown above.
The real purpose of .size() is that it gives you control over loops that you can use in your programs as shown in the next step.
7: Improving Efficiency:
Most of my examples above would not work very efficiently if large amounts needed to be added to a vector.
They are also intended as something you can build off.
Below is a more refined program which makes use of for loops.
import java.util.Vector; //imports vector utility
import java.util.Scanner; //imports scanner utillity
class example{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
Vector<String> myVector=new Vector<String>(10,2); //declare vector
String word=""; //this string will be later added to the vector at 0
//Fill vector
for(int i=0;i<=myVector.size();i++){ //this for loop will run for eternity, unless someone enters EXIT
System.out.print("\n Type EXIT to exit loop , Enter a word :");
word=scan.nextLine(); //reads in String from user
if(word.equals("EXIT")){ //causes loop to exit
break;
}
myVector.add(word); //adds word to vector
}//end of for loop
//Display Contense
for(int k=0;k<myVector.size();k++){ //I'll use a for loop to iterate through the array. The number of iterations is limited by the vector's size
System.out.print("\n "+myVector.get(k));
}
}//end of main
}//end of class
The above program allows a user to fill a vector with values and then displays them back.
I Hope my tutorial will be of some help to anyone looking for information and examples on vectors.
This tutorial really only scratch's the surface of their use's. I'll probably make another on objects,class's and vectors






MultiQuote









|