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 **