You are on page 1of 4

Dream.In.

Code> Programming Tutorials> Java Tutorials


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

Ice(ITB)  
  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". Follow & Share General Discussion
what I cover: Caffeine Lounge
1:Importing, Dream.In.Code
2:Declaration of vectors, Corner Cubicle
3:Adding elements to a vector,
4:Deleting elements in a vector, Follow
5:Accessing an Element. Student Campus
6:Discovering vector size,
7:Improving Efficiency. Software Development

What you need to know: Industry News


Scanner-input from keyboard
for loops - only two in final example Introduce Yourself
Small knowledge of class variables and primitive variables
Nightmare.In.Code
1:Importing;
Ok! so the first thing you need is to import the vector utility from the
standard library. Java Tutorials
I need help with this Programming Help
import java.util.Vector; Array Question C and C++
class example{
public static void main(String []args){ Dijkstra's Algorithm VB.NET

Book Review: Murach's Java


}//end of main Beginning Java with C#
}//end of class NetBeans
Python
Graph Data Structure PHP
2:Declaration of Vectors. Tutorial
Mobile Development
import java.util.Vector;//imports vector utility Swing, Passive Model-
ASP.NET
class example{ View-Presenter in 5
.NET Framework
public static void main(String []args){ minutes.
Ruby
Vector<String> myVector=new Vector<String>(10,2);
Book Review: Murach's
Game Development
Java Servlets and JSP
}//end of main
Assembly
}//end of class Phobos - A JavaFX Games
Databases
Engine: Part 2 - JavaFX
This program declares a Vector of size 10 and its space will increase Scene API and the FSM ColdFusion
by 2 when more then 10 elements are added.
Maven Tutorial 2 - VB6
If it was declared like below
Adding Dependencies Other Languages
Vector<String> myVector=new Vector<String>();
Maven Tutorial 1 - 52 Weeks Of Code

Installation and Getting


Then the starting size would be 10 by default and it would double
everytime it became full Started Web Development
The control over how fast the array grows is used to ensure the vector Phobos - A JavaFX Games
never gets needlessly big. Web Development
The "<String>" declares what variables the Vector can hold. Engine: Part 1 - Intro to
This can be replaced with other class type variables such as "<String>", HTML & CSS
"<Double>","Integer","Object" and so on... but no primitive types like "int" Threading and DP
; JavaScript
221 More Java Tutorials...
3: Adding to a vector Graphic Design
Adding contents to a Vector can be done a number of ways. Flash & ActionScript
The way I'll explain begins with declaring a vector of Strings. Reference Sheets
Blogging
import java.util.Vector; //imports vector utility
class example{ SEO & Advertising
public static void main(String []args){ Web Servers & Hosting
Vector<String> myVector=new Vector<String>(10,2); //declare vector Site Check
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 valu


}//end of main

}//end of class
Code Snippets
C Snippets
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 C++ Snippets
The function ".get()" returns the value of an element at whatever index
is placed inside its brackets. Java Snippets
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. Visual Basic Snippets

The majority of the time I use vectors for storing objects that are C# Snippets
defined by a seperate class program.
FAQ
But I'm| Team
keeping this Blog |asFeedback/Support
as simple | Advertising
possible so maybe that's a tutorial for | Terms
VB.NET Snippets of Use | Privacy Policy |
another day
About UsASP.NET Snippets
4:Deleting Elements
PHP Snippets
To delete an element in an array we use the .remove() command
Python Snippets
Copyright 2001-2017 MediaGroup1 LLC, All Rights Reserved
Ruby Snippets
A MediaGroup1
import java.util.Vector; LLC Production - Version 6.0.2.1.36
//imports vector utility
class example{ ColdFusion Snippets
Server: secure3
public static void main(String []args){ SQL Snippets
Vector<String> myVector=new Vector<String>(10,2); //declare vector
String sample="tester"; //test string declared Assembly Snippets

Functional Programming
myVector.add(sample); //adds sample's value to the vector
Snippets

System.out.println("Value is :"+myVector.get(0)); Perl Snippets


//Displays the valu

myVector.remove(0); //this removes the element at the specifi


HTML/CSS Snippets

}//end of main Javascript Snippets

}//end of class Flash/ActionScript


Snippets

This program above is program is fairly simple. ASP Snippets


I place a string in the vector then remove it with the .remove() function
at its position. Linux, Unix, and Bash
Snippets

5:Accessing an Element. Other Languages


I have already used and explained the .get() function in previous Snippets
examples above.
So I'll show some other methods Regex
First up its .elementAt()

import java.util.Vector;
class example{
//imports vector utility DIC Chatroom
public static void main(String []args){ Join our IRC Chat

Vector<String> myVector=new Vector<String>(10,2); //declare vector

String sample="tester"; //test string declared


Bye Bye Ads
String holder; //Will hold the value of an ele

myVector.add(sample); //adds sample's value to the vector


holder=myVector.elementAt(0);
System.out.println("Value is :"+holder); Dream.In.Code
//Displays
88,135 likes

}//end of main

}//end of class
Like Page Sign Up

This program use's the elementAt() function to extract the value from
the array into a normal variable and then display it
Be the first of your friends to like this
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 vect

String e1="Element1Contense"; //this string will be later a


String e2="Element2Contense"; //this string will be later a

String holder; //this stri


myVector.add(e1); //adds e1 to the ve
myVector.add(e2); //adds e2 to the ve
holder=myVector.toString(); //holder becomes

System.out.println("Value is :"+holder); //Disp

}//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); //dec

String e1="Element1"; //this string will be later a


String e2="Element2"; //this string will be later a

int number; //thi


myVector.add(e1); //adds e1
myVector.add(e2); //adds e2
number=myVector.size(); //number becomes equal

System.out.println("The Number of elements are :"+numb

}//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,

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 arra

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

Replies To: Vectors for Beginners

NeoTifa  
Posted 26 August 2009 - 12:46 PM
Excellent. Maybe they would benfit in knowing the difference between
LinkedList, ArrayList, and Vector.

Ice(ITB)  
Posted 26 August 2009 - 01:37 PM
(//www.dreamincode.net/forums
/index.php?s=6d11c91442bad37e88d3e0eea3f47931&
app=forums&module=forums&section=findpost&pid=747237)
NeoTifa, 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.

The_Programmer-  
Posted 10 December 2011 - 03:26 PM
A Vector seems like an ArrayList. What is the difference?

modi123_1  
Posted 10 December 2011 - 04:20 PM
Good bless the googles.

http://javarevisited...st-in-java.html
(https://javarevisited.blogspot.com/2011/09/difference-vector-vs-
arraylist-in-java.html)
Page 1 of 1
Query failed: unknown local index 'forums_search_posts_main' in search
request.

You might also like