Page 1 of 1

Vectors for Beginners Starts at declaration and runs through some basic functions with examp Rate Topic: -----

#1 Ice(ITB)  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 19-August 09

Post icon  Posted 26 August 2009 - 04:33 AM

Hopfully I'll manage to cover the basics of vectors in this tutorial. Vectors are like the sun website says "a growable array of objects".

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 :)

Is This A Good Question/Topic? 2
  • +

Replies To: Vectors for Beginners

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,450
  • Joined: 24-September 08

Posted 26 August 2009 - 12:46 PM

Excellent. Maybe they would benfit in knowing the difference between LinkedList, ArrayList, and Vector. :) :^:
Was This Post Helpful? 1
  • +
  • -

#3 Ice(ITB)  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 19-August 09

Posted 26 August 2009 - 01:37 PM

View PostNeoTifa, on 26 Aug, 2009 - 11:46 AM, said:

Excellent. Maybe they would benfit in knowing the difference between LinkedList, ArrayList, and Vector. :) :^:


Thanks ^^ and I'll stick that in at the start of the next one... as for this one I wanted to stay as far away from arrays as I could to stop confusion.

;)
Was This Post Helpful? 0
  • +
  • -

#4 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Posted 10 December 2011 - 03:26 PM

A Vector seems like an ArrayList. What is the difference?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8364
  • View blog
  • Posts: 31,073
  • Joined: 12-June 08

Posted 10 December 2011 - 04:20 PM

Good bless the googles.

http://javarevisited...st-in-java.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1