Professional Documents
Culture Documents
Vectors For Beginners - Java Tutorials - Dream - In.Code-1
Vectors For Beginners - Java Tutorials - Dream - In.Code-1
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
}//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
import java.util.Vector;
class example{
//imports vector utility DIC Chatroom
public static void main(String []args){ Join our IRC Chat
}//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
}//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.
}//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.
//Fill vector
for(int i=0;i<=myVector.size();i++){ //this for loop will run for eternity,
//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.
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§ion=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.