Lost Password?

  #1 (permalink)  
Old 12-09-2006, 05:11 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,441
Last Blog:
Object Oriented Design...
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default Java:Tutorial - Arrays

In an earlier tutorial, we looked at how to store a single value to a variable, similarly an array is a named place to store a SET of values allowing you to group common types.

Sometimes, it becomes necessary to order variables in a list to spot trends:

Quote:
Temperature on Sept 1 = 90
Temperature on Sept 2 = 87
Temperature on Sept 3 = 84
This is the ideal application for an array. All the variables and values are of the same type, integers, string, or what ever. Moreover there order and place in the list is important. Since an array allows you to store a “list” of values as a single “variable,” there must be a means of referencing each item. This reference is called an index. So therefore:

If
Code:
Temp[1] = 90
Temp[2] = 87
Temp[3] = 84
Then
Code:
int x = 2;
Temp[ x ] = 87;
At first, a concept that is difficult to grasp is that the index starts at 0. There for, an array with 5 elements contains the index values {0, 1, 2, 3, 4} but how do we make an array?


Code:
int z[ ] = new int[5];
A working example might look like this:
Code:
int x;
int z[] = new int[10];
for (x = 0; x < 10; x++) {
z[x] = x;
}
Therefore z[4] = 4! Arrays do not have to be numbers, for example:

Code:
 int x;
String myArray[ ] = new String[5];
myArray[0] = "hello";
myArray[1] = "this";
myArray[2] = "is";
myArray[3] = "an";
myArray[4] = "array";
for (x=0; x <= 4; x=x+1  )
{
System.out.println( myArray[x] );
}
You can accomplish the same thing in less lines of code by using the following:
Code:
int x;
String myArray[] = {"hello", "this", "is", "an", "array"};
for (x=0; x<=4; x++){
System.out.println( myArray[x] );
}
If a single list of values isnt powerfull enough, we also have the ablilty to use miltidimensional arrays. This is sometimes refrered to as a matrix. Each index has its own set of indicies that are pared with it, for example:

Code:
int matrix[][] = {{1,2,3,4}, {4,5,6,7}, {7,8,9,10}};
for (int i=0; i<=2; i++){
for (int j=0; j<=3; j++){
System.out.println( matrix[i][j] );
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-17-2008, 10:56 AM
Turk4n's Avatar   
Turk4n Turk4n is offline
Programmer
 
Join Date: May 2008
Location: ?
Age: 18
Posts: 110
Rep Power: 3
Turk4n has a spectacular aura aboutTurk4n has a spectacular aura aboutTurk4n has a spectacular aura about
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Smile Re: Java:Tutorial - Arrays

I just had to reply to this wonderful thread!

I mean Wow, normally people just make random explanations with no real sense; But you made one with quite awsome style, thank you for this great thread.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript:Tutorial, Using Arrays TcM Javascript 20 06-26-2008 02:45 PM
Python arrays... Sir_Rimo Python 3 06-20-2007 08:54 AM
Arrays clookid PHP Tutorials 1 01-11-2007 08:30 PM
Arrays Sionofdarkness C and C++ 5 07-26-2006 05:35 PM
Arrays in NET 2.0 or CLI Managed C++ Void Managed C++ 1 07-18-2006 07:57 PM


All times are GMT -5. The time now is 07:32 PM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads