[TUTORIAL] JAVA - Lists, Arrays and loops
Posted: Mon May 31, 2010 11:08 pm
Since I'm on a roll, here's another little tutorial for our wanna be java developers.
This is very basic stuff that anyone who wishes to code in Java should know, wether it's on L2J or elsewhere.
I will not cover maps here today because they are a bit more complex in nature and I want to stick to the simple stuff.
I'll try to cover as much of the basics as possible without making this heavy to read or learn, if you wish to dig further into this, I would recommend you read this: http://java.sun.com/docs/books/tutorial ... index.html
1- Lists:
A list in java contains a series of objects, usually of the same type.
Lists work with indexes starting at 0 and increasing, which means that if your list contained:
{"a","f","h","u"}
a would be at index 0
f would be at index 1
etc...
Here's of how to initialize a list:
We have created our list, but for now it's empty.
There are a few basic methods that you can call on a list which you absolutely need to be aware of:
.isEmpty() Will return true if the list is empty
.get(index) Will return the object at the index specified
.remove(index) Will remove the object at the specified index
.add(object) Will add the specified object at the end of the list, providing that its type is compatible with the type set when initializing the list
.toArray() Converts your list into an Array of the same type
.contains(object) Will return true if this list contains that object
1- Arrays:
Arrays are very similar to lists in nature, in that they are also a list of objects.
However, arrays are not as flexible. They have a pre-determined lenght and are not as flexible as lists.
Here's a few ways how to initialize an array:
Both arrays would have a set lenght of 3, which is the maximum number of items that can be placed in this array.
To modify an object in an array, you need to call it's index like so:
So you will ask: "Why would we want to use arrays when Lists are more flexible and dont have so much limitations?"
The answer is simple: When you know ahead of time how many objects you will need in a list, and you know that you will have to do iterations over it(loops... see further down) or you will need to retrieve/modify objects often, arrays are usually a better choice because they are simply faster.
http://jayant7k.blogspot.com/2008/05/ja ... rs-vs.html
http://robaustin.wikidot.com/how-does-t ... e-to-array
3- Loops:
There are mainly 2 types of loops used in Java(I will not cover "do" here):
while => an infinite loop until a certain condition is met
for => a loop which iterates through a list(collection) of objects
Lets start with for:
The "for" loop can be used in 2 ways, the first one involves iterating through a collection(list/array) of objects.
Lets take a look at a quick example:
In this case the loop will run 4 times, going through the "names" array until it finds the String "Gonzo", it will then print "Hello Gonzo! xD" on the console.
The second way of using the "for" loop is to go through a series of numeric(integer) values like so:
Lets take a quick look at the "for" statement:
for(int i=0; i<4; i++)
The first thing we did is declare the "i" variable as being an integer and setting its base value to "0"
Then we added "i<4", which basically says that "i" cannot be higher than 3, once it reaches 4 the loop will be over.
And finally we added "i++", which means that each time the loop goes through, 1 is added to the value of "i" (in simple, i = i+1)
So in essence, the loop will run 4 times(from 0 to 3) and perform whatever action we set inside it.
Now lets take a look at the other kind of loop, the "while" loop:
The while loop will run indefinitely until the condition it's looking for returns false.
In essence, you could even make a real infinite loop by doing this:
As it would never return false, it would keep going through the loop indefinitely.
Most times you will need to set a condition like so:
It's really as simple as this, I dont think I need to add anything more to cover the basics
Happy coding everyone!
** Once again, if L2J devs see something missing or want to correct something, please feel free to do it directly on this post **
This is very basic stuff that anyone who wishes to code in Java should know, wether it's on L2J or elsewhere.
I will not cover maps here today because they are a bit more complex in nature and I want to stick to the simple stuff.
I'll try to cover as much of the basics as possible without making this heavy to read or learn, if you wish to dig further into this, I would recommend you read this: http://java.sun.com/docs/books/tutorial ... index.html
1- Lists:
A list in java contains a series of objects, usually of the same type.
Lists work with indexes starting at 0 and increasing, which means that if your list contained:
{"a","f","h","u"}
a would be at index 0
f would be at index 1
etc...
Here's of how to initialize a list:
Code: Select all
List<String> names = new ArrayList<String>();
There are a few basic methods that you can call on a list which you absolutely need to be aware of:
.isEmpty() Will return true if the list is empty
.get(index) Will return the object at the index specified
.remove(index) Will remove the object at the specified index
.add(object) Will add the specified object at the end of the list, providing that its type is compatible with the type set when initializing the list
.toArray() Converts your list into an Array of the same type
.contains(object) Will return true if this list contains that object
1- Arrays:
Arrays are very similar to lists in nature, in that they are also a list of objects.
However, arrays are not as flexible. They have a pre-determined lenght and are not as flexible as lists.
Here's a few ways how to initialize an array:
Code: Select all
String[] names = {"Jack","Luke","Lisa"};
Code: Select all
String[] names = new String[3];
To modify an object in an array, you need to call it's index like so:
Code: Select all
names[0] = "Maria";
The answer is simple: When you know ahead of time how many objects you will need in a list, and you know that you will have to do iterations over it(loops... see further down) or you will need to retrieve/modify objects often, arrays are usually a better choice because they are simply faster.
http://jayant7k.blogspot.com/2008/05/ja ... rs-vs.html
http://robaustin.wikidot.com/how-does-t ... e-to-array
3- Loops:
There are mainly 2 types of loops used in Java(I will not cover "do" here):
while => an infinite loop until a certain condition is met
for => a loop which iterates through a list(collection) of objects
Lets start with for:
The "for" loop can be used in 2 ways, the first one involves iterating through a collection(list/array) of objects.
Lets take a look at a quick example:
Code: Select all
String[] names = {"Gertrude","Camilia","Gonzo","Lydia"};for(String name: names){ if(name.equals("Gonzo")) { System.out.println("Hello Gonzo! xD"); }}
The second way of using the "for" loop is to go through a series of numeric(integer) values like so:
Code: Select all
for(int i=0; i<4; i++){ if(i == 2) { System.out.println("== 2 =="); }}
for(int i=0; i<4; i++)
The first thing we did is declare the "i" variable as being an integer and setting its base value to "0"
Then we added "i<4", which basically says that "i" cannot be higher than 3, once it reaches 4 the loop will be over.
And finally we added "i++", which means that each time the loop goes through, 1 is added to the value of "i" (in simple, i = i+1)
So in essence, the loop will run 4 times(from 0 to 3) and perform whatever action we set inside it.
Now lets take a look at the other kind of loop, the "while" loop:
The while loop will run indefinitely until the condition it's looking for returns false.
In essence, you could even make a real infinite loop by doing this:
Code: Select all
while(true){ // Your code here...}
Most times you will need to set a condition like so:
Code: Select all
while(player.isInCombatStance()){ // Your code here...}
Happy coding everyone!
** Once again, if L2J devs see something missing or want to correct something, please feel free to do it directly on this post **