[TUTORIAL] JAVA - Lists, Arrays and loops

Find all FAQs, HOWTOs and guides here.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
theone
Posts: 1384
Joined: Tue Aug 12, 2008 2:28 am
Location: Quebec, Canada

[TUTORIAL] JAVA - Lists, Arrays and loops

Post by theone »

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:

Code: Select all

List<String> names = new ArrayList<String>();
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:

Code: Select all

String[] names = {"Jack","Luke","Lisa"};

Code: Select all

String[] names = new String[3];
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:

Code: Select all

names[0] = "Maria";
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:

Code: Select all

String[] names = {"Gertrude","Camilia","Gonzo","Lydia"};for(String name: names){    if(name.equals("Gonzo"))    {        System.out.println("Hello Gonzo! xD");    }}
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:

Code: Select all

for(int i=0; i<4; i++){    if(i == 2)    {        System.out.println("== 2 ==");    }}
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:

Code: Select all

while(true){    // Your code here...}
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:

Code: Select all

while(player.isInCombatStance()){    // Your code here...}
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 :) **
Last edited by theone on Tue Jun 01, 2010 9:44 pm, edited 3 times in total.
Image
andr3iu7z
Posts: 114
Joined: Sun Jan 10, 2010 2:25 pm

Re: [TUTORIAL] JAVA - Lists, Arrays and loops

Post by andr3iu7z »

Oh my god theone!
you're the best!
I like what you are doing for this community and for l2j developing community!
big heart xD
User avatar
DrHouse
L2j Inner Circle
L2j Inner Circle
Posts: 912
Joined: Mon Jan 22, 2007 12:14 am
Location: Spain

Re: [TUTORIAL] JAVA - Lists, Arrays and loops

Post by DrHouse »

Hey, thanks again for dropping a bit of your knowledge over here :)

I have just a couple comments, by the way:

1) As far as I know this will throw syntax error:

Code: Select all

String[3] names; // wrongString[] names; // right
Also, this will not initialize any array, nor any memory will be allocated. It is just a reference, a pointer :) . I think this is a very important difference even for newbies

2) Just a little further important (imho) info about lists: Not all lists have got an array on backstage, so elements are not "indexed" at all. For instance, linked lists, are working with linked nodes. Maybe this is a bit advanced at the begining but there is an important difference between them. Some implementations doesnt have even get(int index) method
Image

Leadership and management are not talk and talk, but talk and do

Proud of being a part of this project
User avatar
theone
Posts: 1384
Joined: Tue Aug 12, 2008 2:28 am
Location: Quebec, Canada

Re: [TUTORIAL] JAVA - Lists, Arrays and loops

Post by theone »

DrHouse wrote:

Code: Select all

String[3] names; // wrongString[] names; // right
Thx for the correction, you're 100% right :D
I looked at what I wrote and thought =>M3 iZ DuMbZ<=
Proper way to do this would be(and actually initialize the variable without passing data into it):

Code: Select all

	Integer[] a = new Integer[3];ย 
First post updated
DrHouse wrote:2) Just a little further important (imho) info about lists: Not all lists have got an array on backstage, so elements are not "indexed" at all. For instance, linked lists, are working with linked nodes. Maybe this is a bit advanced at the begining but there is an important difference between them. Some implementations doesnt have even get(int index) method
Yeah you're right again, I put the link to the Collections tutorial for that :)
But I wanted to start them up with ArrayList only as it's the most basic and flexible type of list(and also the fastest in alot of cases) and it's what they'll need to use most of the time unless they are doing something precise.
For example, SortedList could be bypassed using Collections.sort(arraylist);
But Dr House is right you guys, once you understand the basics you should take a look at the Collections tutorial list I posted and dig in further.
Image
User avatar
DrHouse
L2j Inner Circle
L2j Inner Circle
Posts: 912
Joined: Mon Jan 22, 2007 12:14 am
Location: Spain

Re: [TUTORIAL] JAVA - Lists, Arrays and loops

Post by DrHouse »

Sure, it is quite difficult set the borders for a tutorial. Howerver, you look really skilly to sumarize the most important stuff
Image

Leadership and management are not talk and talk, but talk and do

Proud of being a part of this project
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7005
Joined: Tue Aug 11, 2009 3:36 am

Re: [TUTORIAL] JAVA - Lists, Arrays and loops

Post by Zoey76 »

DrHouse wrote:2) Just a little further important (imho) info about lists: Not all lists have got an array on backstage, so elements are not "indexed" at all. For instance, linked lists, are working with linked nodes. Maybe this is a bit advanced at the begining but there is an important difference between them. Some implementations doesnt have even get(int index) method
I think that even LinkedList which is not backed by an array, implements get(index) method, with O(n), it's not as effective as ArrayList#get(index) which has O(1) to access the element by index.

If it implements the interface List, it's most likely that it implements the get(index) method
Powered by Eclipse 4.30 ๐ŸŒŒ | Eclipse Temurin 21 โ˜• | MariaDB 11.3.2 ๐Ÿ—ƒ๏ธ | L2J Server 2.6.3.0 - High Five ๐Ÿš€

๐Ÿ”— Join our Discord! ๐ŸŽฎ๐Ÿ’ฌ
Post Reply