Forum Topic: Java: arrays

(645 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

seel

Reply To Post Reply & Quote

Posted at: 6/8/06 01:08 PM

seel FAB LEVEL 19

Sign-Up: 06/27/05

Posts: 2,193

Java: Main

Hello! I guess you are here cause you want to learn about the array basics in java. If that aint the case.. Your a confused/really stoned man..

Anyway, it might be a good idea to start with this tutorial now right? So, arrays, what are they? An array is a piece of code that lets you store many values in one place. Instead of declaring the values 1,2,3,4,5,6,7,8,9 and 0 in a variable each, you can use an array. To do so, you have to write in the code for the array. First you have to initialise the array with something like this:

int[] myArray;
myArray = new int[10];

the most of this part will be explained later..

Then the code for the array itself:

int[] myArray = {1,2,3,4,5,6,7,8,9,0};

Now im going to explain it as good as i can..

int[]
Tells the program that a certain thing (Like an array or a variable) is of the type int that can store numbers (Limited lenght). int for integer.

myArray
This is just the name of the array.

=
Its kinda obvious what this one does.. (Telling the program that the following will be assigned to the instance to the left.)

{1,2,3,4,5,6,7,8,9,0}
These are the values that are assigned to the array. each value is separated from others with the "," (comma). The curly brackets are just the begining and the end of the value/statement that is assigned to a function or something.. (In this case its values).

;
The semicolon is the end for the part of code that has it at the end. It tells the program that "Here is the end of this code, now start reading the next one.". Without the semicolon you will get an error since the program reads the line, then it continues to the next line that is something not relevant to the first.

Then we have the new int[10]
new is obvious, it tells that its something new :P . The int[10] gives the array myArray a max size of 10 values, all values after the first ten wont be accesible in the program.

To make it easier for you to understand arrays, heres some help:

First statement: The data type of the array, integer for example.
Second: the_name_of_the_array

Third: The position of the values in the array
0 first value
1 second value
2 third value
and so on..

The values are called "Data".

So, you want the whole code?? I wont give it to you :)

nah just kidding (lol :P ). Here it is:

int[] myArray;
myArray = new int[10];

int[] myArray = {1,2,3,4,5,6,7,8,9,0};

Thats it for the array basics, happy codeing everyone! :D


None

seel

Reply To Post Reply & Quote

Posted at: 6/8/06 04:14 PM

seel FAB LEVEL 19

Sign-Up: 06/27/05

Posts: 2,193

Ops forgot:
If you want to, you dont have to fill in any values for an array but give it a size, like int[] myArray = new int[10]; w/o assigning any values to it. You dont have to declare a size for an array either(aslong as you initialize an array). Just leave the size in the int[] myArray = new int[*look its empty"] empty and you can extend the number of values inside the brackets. Like from int[] myArray = {1,2} to int[] myArray = {1,2,3,4} and the size is bigger. But be careful with not declareing a limited number of values, you might get some unpleasant surprises if you dont give it a limit of size..

(REMEMBER THAT THIS IS ONLY BASIC ARRAYS, THE ADVANCED MIGHT JUST COME LATER)

None

kidray76

Reply To Post Reply & Quote

Posted at: 6/8/06 04:17 PM

kidray76 FAB LEVEL 36

Sign-Up: 10/19/05

Posts: 6,453

Yeah, array's not hard to explain at all. In fact, most people who are good at java script arlready know about array. Quick lesson though. Now, talk about inheritance and polymorphism next. That'll get some replies.

NG Review Moderator // Pm me for Review Abuse

BBS Signature

None

seel

Reply To Post Reply & Quote

Posted at: 6/8/06 04:39 PM

seel FAB LEVEL 19

Sign-Up: 06/27/05

Posts: 2,193

At 6/8/06 04:17 PM, kidray76 wrote: Yeah, array's not hard to explain at all. In fact, most people who are good at java script arlready know about array. Quick lesson though. Now, talk about inheritance and polymorphism next. That'll get some replies.

Well its always good to make these kinda tutorials for the mains, since there is always ppl who are new to the programing language and they might have some good use of this kinda basic knowledge :). I have only been using java for like 2 weeks so i aint to good at it (I have been using C++ and visual J# before so it didn't take long to learn most stuff..).


None

Craige

Reply To Post Reply & Quote

Posted at: 6/8/06 07:19 PM

Craige LIGHT LEVEL 08

Sign-Up: 07/17/04

Posts: 3,072

You should work on your statement order/compilation (your writing that is, not tho Java). This was a little hard to read because some things were not explained well, or you jumped arround a bit. Also, keep away from saying stuff like "it is done simply like..." It is not needed, and it really draws one away frow reading. Instead, form a little more complex, and slightly longer sentences. ie: "You would do this like so:" There is no real difference, except in the pleasure of reading it for the reader, and that is who this is for after all.

It appears you knew the material you were trying te teach, but were just unable to express it in a readable way. Work on that, and your turorials should improve.


None

amaterasu

Reply To Post Reply & Quote

Posted at: 6/8/06 10:54 PM

amaterasu LIGHT LEVEL 07

Sign-Up: 03/07/04

Posts: 2,059

At 6/8/06 04:14 PM, seel wrote: Ops forgot:
If you want to, you dont have to fill in any values for an array but give it a size, like int[] myArray = new int[10]; w/o assigning any values to it. You dont have to declare a size for an array either(aslong as you initialize an array). Just leave the size in the int[] myArray = new int[*look its empty"] empty and you can extend the number of values inside the brackets. Like from int[] myArray = {1,2} to int[] myArray = {1,2,3,4} and the size is bigger. But be careful with not declareing a limited number of values, you might get some unpleasant surprises if you dont give it a limit of size..

(REMEMBER THAT THIS IS ONLY BASIC ARRAYS, THE ADVANCED MIGHT JUST COME LATER)

seel, you can't declare an array without a dimension (size), and you can only initialize an array with the brackets upon declaration. I'm not sure if you didn't realize this, or you are trying to say something else. If you have an array where the size varies, you should either use a large partially filled array, but the best choice is either the Vector class or ArrayList class.

NexusTK Characters: Stegmann Cirucci Ulquiorra Ganju

Do you like chill music? Check out my latest work!

BBS Signature

None

amaterasu

Reply To Post Reply & Quote

Posted at: 6/8/06 10:59 PM

amaterasu LIGHT LEVEL 07

Sign-Up: 03/07/04

Posts: 2,059

Curly brackets. You can only use the curly brackets upon declaration.

NexusTK Characters: Stegmann Cirucci Ulquiorra Ganju

Do you like chill music? Check out my latest work!

BBS Signature

None

seel

Reply To Post Reply & Quote

Posted at: 6/9/06 05:56 AM

seel FAB LEVEL 19

Sign-Up: 06/27/05

Posts: 2,193

Oh noes! I think i wasn't clear.... =/


None

Lidov

Reply To Post Reply & Quote

Posted at: 6/11/06 01:19 PM

Lidov LIGHT LEVEL 25

Sign-Up: 02/09/05

Posts: 4,589

Maybe you also should have mentioned that there is the possibility to ask a certain array about its number of places (how should I call it?). You can do this by writing myArray.length and you can use it as a regular int. It is very good if you want to do something in an array whose length is unknown. For example:

for(int i=0;i<=myArray.length;i++) //the purpose of this is to put 0 in all of the array.
{
myArray[i]=0;
}


None

StarCleaver

Reply To Post Reply & Quote

Posted at: 6/11/06 03:24 PM

StarCleaver LIGHT LEVEL 29

Sign-Up: 01/03/03

Posts: 10,102

May as well throw in the Arrays class, which contains useful operations for arrays.

I could surely die
If I only had some pie
Club-a-Club Club, son

BBS Signature

None

Lidov

Reply To Post Reply & Quote

Posted at: 6/11/06 04:28 PM

Lidov LIGHT LEVEL 25

Sign-Up: 02/09/05

Posts: 4,589

At 6/11/06 03:24 PM, Star_Cleaver wrote: May as well throw in the Arrays class, which contains useful operations for arrays.

Wow, this link could help me a lot, I can't believe I used to write a whole code for doing the things I could easily do with this simple class.


All times are Eastern Standard Time (GMT -5) | Current Time: 12:38 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!