[TUTORIAL] Vectors [JAVA]
AirTrake :: Development :: Java
Page 1 of 1 • Share •
[TUTORIAL] Vectors [JAVA]
Basically vectors are arrays, just that their size is dynamic. Arrays are just data containers. But most of you should know them from C/C++ already.
First of all you will need to import the vector class:
Now you have to create a vector. This is done by creating a class object from the Vector class:
In this example I defined the size to be 20. If you would leave that empty it was 10 by default. Integer is just the type of variables we want to store in our vector.
To make everything a bit clearer I also spread some console output over my program:
You can use System.out.println etc., it really doesn't matter. The output of this would be:
Of course does it contain 0 items at the moment. And the size is 20, just like we defined it to be earlier.
For the next step I have added a simple for-loop to add some items to my vector:
This will (of course) add 40 items. Please note that our defined size is 20, so it was designed to hold 20 items. So that means this would be over our limit. But here comes the advantage of vectors in place. When you do the same output as before, you will get this text:
will give us:
That means the size of our vector got changed automaticly.
----------------------------------------------------------------------------------------------
Part 2:
----------------------------------------------------------------------------------------------
Every item inside a vector has an index. This index starts with 0, not with 1 as some of you might think now.
To explain this all I have created a new vector, this time it has the type String.
I did not define the size of my vector yet, so I have left this field blank.
Now I am going to add some elements to it. In the former part we have added integers to it, but now I wanna add some String - elements to it.
Just like the comment shows: The element "Johnny" has an index of 0, Susi, in this case our last element, an index of 2.
With the mehtod "get" of the vector class we can easily get an elemtn by its index, example:
This will show us that the element "Johnny" has the index 0. However, it is often the case that you would want this process reverse. That means: Get the index of an element. For this tutorial I have created an integer which will store this index then. Remember: Vectors start with 0, not with 1.
The method "indexOf" will return the index of a specific element in your vector:
Important: The element name is case sensitive, writing there "ToMmy" for example will give you an index with the value of -1. This means that it is not in the vector, so an invalid element.
The element "Tommy" should have the index 1. Let's check it with printing it to our console:
First of all you will need to import the vector class:
- Code:
import java.util.Vector;
Now you have to create a vector. This is done by creating a class object from the Vector class:
- Code:
Vector<Integer> v = new Vector<Integer>(20);
In this example I defined the size to be 20. If you would leave that empty it was 10 by default. Integer is just the type of variables we want to store in our vector.
To make everything a bit clearer I also spread some console output over my program:
- Code:
out("[INFO] Vector capacity (old): " + v.capacity());
out("[INFO] Vector contains " + v.size() + " item(s)");
You can use System.out.println etc., it really doesn't matter. The output of this would be:
[INFO] Vector capacity (old): 20
[INFO] Vector contains 0 item(s)
Of course does it contain 0 items at the moment. And the size is 20, just like we defined it to be earlier.
For the next step I have added a simple for-loop to add some items to my vector:
- Code:
for(int i = 0; i < 40; i++)
v.add(i);
This will (of course) add 40 items. Please note that our defined size is 20, so it was designed to hold 20 items. So that means this would be over our limit. But here comes the advantage of vectors in place. When you do the same output as before, you will get this text:
- Code:
out("=>[INFO] Vector contains " + v.size() + " item(s)");
out("=>[INFO] Vector capacity: "+v.capacity());
will give us:
=>[INFO] Vector contains 40 item(s)
=>[INFO] Vector capacity: 40
That means the size of our vector got changed automaticly.
----------------------------------------------------------------------------------------------
Part 2:
----------------------------------------------------------------------------------------------
Every item inside a vector has an index. This index starts with 0, not with 1 as some of you might think now.
To explain this all I have created a new vector, this time it has the type String.
- Code:
Vector<String> v = new Vector<String>();
I did not define the size of my vector yet, so I have left this field blank.
Now I am going to add some elements to it. In the former part we have added integers to it, but now I wanna add some String - elements to it.
- Code:
v.add("Johnny");//Idx 0
v.add("Tommy");//Idx 1
v.add("Susi");//Idx 2
Just like the comment shows: The element "Johnny" has an index of 0, Susi, in this case our last element, an index of 2.
With the mehtod "get" of the vector class we can easily get an elemtn by its index, example:
- Code:
System.out.printf("Element = %s\n",v.get(0));
This will show us that the element "Johnny" has the index 0. However, it is often the case that you would want this process reverse. That means: Get the index of an element. For this tutorial I have created an integer which will store this index then. Remember: Vectors start with 0, not with 1.
- Code:
int index = -1;
The method "indexOf" will return the index of a specific element in your vector:
- Code:
index = v.indexOf("Tommy");
Important: The element name is case sensitive, writing there "ToMmy" for example will give you an index with the value of -1. This means that it is not in the vector, so an invalid element.
The element "Tommy" should have the index 1. Let's check it with printing it to our console:
- Code:
System.out.println("This element has the index: "+ index);
Last edited by Giovanni on Wed Jan 25, 2012 7:08 pm; edited 2 times in total
Giovanni- Newbie
- Posts: 9
Join date: 2012-01-02
Location: Doenerland
Re: [TUTORIAL] Vectors [JAVA]
Dynamic memory management is an important topic, nice that you've introduced vecotors! 
You might also explain "dynamic list", how is that working on Java?
I've already an example for c++:
Java doesn't use pointers, so how's it working there?
You might also explain "dynamic list", how is that working on Java?
I've already an example for c++:
Java doesn't use pointers, so how's it working there?
Re: [TUTORIAL] Vectors [JAVA]
Yadin wrote:Dynamic memory management is an important topic, nice that you've introduced vecotors!
You might also explain "dynamic list", how is that working on Java?
I've already an example for c++:
Java doesn't use pointers, so how's it working there?
I am not sure if I am thinking of the same thing as you now, and I also did not go through your tutorial closely since I am not really experienced with C/C++ but vectors are basically dynamic arrays (dynamic lists).
However, I have updated the tutorial. It shows some more methods of the vector class and how to use them.
Giovanni- Newbie
- Posts: 9
Join date: 2012-01-02
Location: Doenerland
Re: [TUTORIAL] Vectors [JAVA]
Ye thanks, can you make a tutorial about Arrays and Strings (using and coverting them), that's what I need right now! 
Similar topics» Easy Paint Tool SAI - Tutorial
» [Tutorial]Wie man mit GIMP Felle erstellen kann
» Sections - Ein Schritt-Für-Schritt Tutorial
» WettRennen Tutorial
» Haus Bau Tutorial
» [Tutorial]Wie man mit GIMP Felle erstellen kann
» Sections - Ein Schritt-Für-Schritt Tutorial
» WettRennen Tutorial
» Haus Bau Tutorial
AirTrake :: Development :: Java
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum