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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| 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; Code: Code: int z[ ] = new int[5]; Code: Code:
int x;
int z[] = new int[10];
for (x = 0; x < 10; x++) {
z[x] = x;
} 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] );
} Code: Code:
int x;
String myArray[] = {"hello", "this", "is", "an", "array"};
for (x=0; x<=4; x++){
System.out.println( myArray[x] );
} 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] );
}
} |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |
Our Partners |