IT Community - Software Programming, Web Development and Technical Support

Java:Tutorial - Arrays

This is a discussion on Java:Tutorial - Arrays within the Java Programming forums, part of the Software Development category; In an earlier tutorial, we looked at how to store a single value to a variable, similarly an array is ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-23-2007, 11:54 PM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
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:
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:
Code:

int z[ ] = new int[5];
A working example might look like this:
Code:
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:
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:
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:
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] );
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

vB 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
Arrays in Java leoraja8 Java Programming 7 11-19-2007 12:23 AM
Java:Tutorial - A better looking GUI pranky Java Programming 2 03-08-2007 11:34 AM
Java:Tutorial - The Variable pranky Java Programming 0 02-23-2007 11:59 PM
Java:Tutorial - Getting Started pranky Java Programming 0 02-23-2007 11:48 PM
Java:Tutorial - Tic-Tac-Toe pranky Java Programming 6 02-23-2007 08:48 AM


All times are GMT -7. The time now is 02:58 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0