September 07, 2010, 04:15:42 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Learn Arrays and other various things [Tutorial] (RSPS)  (Read 916 times)
0 Members and 1 Guest are viewing this topic.
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« on: July 31, 2009, 08:41:39 AM »

This will teach you how to add a new file to your server which will handle specifically arrays. Although this isn't the best of an idea, this is just an example - and if you read through this whole tutorial, you could actually learn something.  But some may not know what an Array is?

Definition:


What you will learn: Arrays, Integers, Static Methods, Creating a file.
Now, perhaps some wonder if this is a Java tutorial or even a server tutorial.  The truth is, it's kind of both.

Lets start by creating a new file.
Every file must begin with:

Code:
public class
We will make this file public because we want it to be seen by other files.  If what so ever you don't, then you would make it private. 

Let's begin to add the first line to the file:

Code:
public class ClientArray {

As you can see, you already know what the file will be called.  (ClientArray).  I've named it that because this file will only handle arrays from the client class.  If you want to modify it, you can change the name.

Although there isn't much to this file, I'll assume that most sources have at least one array already built.

Lets begin to add this, don't worry - it'll change just a bit but it won't give us errors.

Code:
public static final int PACKET_SIZES[] = {

You may wonder why it's in captials this time?
Well, the proper way to do it is whenever a constant is equal to 'public static final', the value should be in capitals.

This isn't full, but already it tells us that this array will hold integers (numbers), and is static.  (Although, static can only be called to a static, this will ease things up by just referring to the class, then method. 

Anyways, the full method is this:



Now, we are missing one bracket to end the file.
So, at the end of the file, add one more bracket.

Code:
}

Now, this is the time where we delete this array from the client class.
In client.java, find this:

Code:
public static final int packetSizes[] = {

Erase the whole thing, as we added it to the file we've just created.
But it still won't work yet, since we've erased it, the method that calls it doesn't know where it is.

So in the same file, search for:

Code:
packetSizes

You will only get one search result for that.
Now, replace 'packetSizes' with:

Code:
ClientArray.PACKET_SIZES

We add this because it will tell the file to go to ClientArray.PACKET_SIZES.
As said before, we need to first give the Class we are going in, and the methods name.

(For those who don't know what the line should look like, here it is:)

Code:
PACKET_SIZES = ClientArray.PACKET_SIZES[packetType];

Alright, but let's say you wanted to create your own array, not just take from whats already there.
As said before, arrays can hold multiple values.

But lets focus on the arrays that hold integers (numbers).
If you're adding for the file we've created, then it would look like this:

Code:
public static int[] example =  {

If your doing something for the client class, then it wouldn't need a static.

Code:
public int[] example =  {

Let's say that we wanted this array to represent untradable items.
Well, what we would first have to do for the array, is add some numbers.

But we can't tell an array what the numbers mean, we can just add the values.

Example for the static array:
Code:
public static int[] untradable = {  //Example
1507, 995
};
Example for the non-static array:
Code:
public int[] untradable = {  //Example
1507, 995
};

For those who are in ClientArray.java, you can close it and open up Client.java.  And those who did this example in Client.java, well - you're in the right place.

As I said before, we are doing 'untradable items' as an example.
We'll have to create a new method, so at the end of the file but before the last bracket add this:

Code:
public boolean isUntradable(int item) {
  for(int i = 0; i < ClientArray.untradable.length; i++) {
   if(ClientArray.untradable[i] == item)
    return true;
  }
 return false;
}

Maybe in my next lesson I'll go a bit further off topic from Array's, but lets focus on this stuff first.
By looking at this method, already you might have guessed that it has something to do with going into the ClientArray class, and going to the untradable method. 

(How do you know?)
Code:
ClientArray.untradable.length

Well, it first sets an integer to 0, but any number that is higher then what we've added in the array, it'll then do:

Code:
if(ClientArray.untradable[i] == item)

What that means is basically, if any number from the array equals an items ID number, then it will return a true statement.  And in this example, if it's true - then the item is untradable, if it's false then it isn't.

Alright, now you've created your own array, but now its your turn to call upon the method. (isUntradable)
Anyways, good luck with this - and I hope you learned something new...

-Ikram
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
Javaforums.net :: a community about Java software development.
« on: July 31, 2009, 08:41:39 AM »

 Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« Reply #1 on: August 01, 2009, 12:29:07 PM »

Looking good, maybe include Lists and generic types? I often see people here asking about how to use them so it might be a good to make a tutorial about it as well.
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
   

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.286 seconds with 36 queries.

Google visited last this page Yesterday at 07:49:37 PM