/*
* Copyright © 2008 - 2009 it.ibluespace.com. All rights reserved.
*/
public class ArrayExample
{
public static void main(String args[])
{
//To create and initialize an array of 3 String objects.
String [] strArray = {"Red", "Green", "Blue"};
//To create an array of 4 integers.
int [] intArray = new int [4];
//An array is accessed by its index.
//An array index is an Integer ranging from 0 to the array length - 1 .
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
//The length of an array is the element number in the array.
//To get the length of the array.
int strArrayLength = strArray.length;
System.out.println("The length of strArray is: " + strArrayLength);
System.out.println("strArray[0] = " + strArray[0]);
System.out.println("strArray[1] = " + strArray[1]);
System.out.println("strArray[2] = " + strArray[2]);
System.out.println();
//To get the length of the array.
int intArrayLength = intArray.length;
System.out.println("The length of intArray is: " + intArrayLength);
System.out.println("intArray[0] = " + intArray[0]);
System.out.println("intArray[1] = " + intArray[1]);
System.out.println("intArray[2] = " + intArray[2]);
System.out.println("intArray[3] = " + intArray[3]);
System.out.println();
//Array of Array
int [][] matrix = { { 1, 2, 3 },
{ 4, 5, 6} };
//matrix[0] is actually an array with value { 1, 2, 3 };
//so the length of matrix[0] is 3 which is the column count of the array
System.out.println("matrix[0][0] = " + matrix[0][0]);
System.out.println("matrix[0][1] = " + matrix[0][1]);
System.out.println("matrix[0][2] = " + matrix[0][2]);
//matrix[1] is actually an array with value { 4, 5, 6};
//so the length of matrix[1] is 3 which is the column count of the array.
System.out.println("matrix[1][0] = " + matrix[1][0]);
System.out.println("matrix[1][1] = " + matrix[1][1]);
System.out.println("matrix[1][2] = " + matrix[1][2]);
System.out.println();
//To get the row count of array matrix.
int matrixRowCount = matrix.length;
//To get the row count of array matrix.
int matrixColCount = matrix[0].length;
System.out.println("matrix row count = " + matrixRowCount);
System.out.println("matrix column count = " + matrixColCount);
}
}
|