class SingleDimensionalArrayDemo1
{
public static void main(String[] args)
{
int[] intArray = new int[5];// declaration and instantiation
intArray[0] = 110;// initialization
intArray[1] = 220;
intArray[2] = 750;
intArray[3] = 130;
intArray[4] = 150;
for (int i = 0; i < intArray.length; i++)
{
System.out.println("Element at index position " + i + " : " + intArray[i]);
}
}
}
Output
Element at index position 0 : 110
Element at index position 1 : 220
Element at index position 2 : 750
Element at index position 3 : 130
Element at index position 4 : 150
SingleDimensionalArrayDemo2.java
public class SingleDimensionalArrayDemo2
{
public static void main(String[] args)
{
int intArray[] =
{ 51, 61, 89 };// declaration, instantiation and initialization
for (int i = 0; i < intArray.length; i++)
{
System.out.println("Element at index position " + i + " : "
+ intArray[i]);
}
}
}
Output
Element at index position 0 : 51
Element at index position 1 : 61
Element at index position 2 : 89
MultidimensionalArrayDemo1.java
public class MultidimensionalArrayDemo1
{
public static void main(String[] args)
{
int[][] intArray = new int[2][3];// 2 rows and 3 columns
intArray[0][0] = 1;
intArray[0][1] = 2;
intArray[0][2] = 3;
intArray[1][0] = 4;
intArray[1][1] = 5;
intArray[1][2] = 6;
int rows = intArray.length;
for (int i = 0; i < rows; i++)
{
int columns = intArray[i].length;
System.out.println("Number of columns in row " + i + " is : "
+ columns);
for (int j = 0; j < columns; j++)
{
System.out.println("Element at " + "intArray[" + i + "]" + "["
+ j + "] : " + intArray[i][j]);
}
System.out.println();
}
}
}
Output
Number of columns in row 0 is : 3
Element at intArray[0][0] : 1
Element at intArray[0][1] : 2
Element at intArray[0][2] : 3
Number of columns in row 1 is : 3
Element at intArray[1][0] : 4
Element at intArray[1][1] : 5
Element at intArray[1][2] : 6
MultidimensionalArrayDemo2.java
public class MultidimensionalArrayDemo2
{
public static void main(String[] args)
{
/*
* declare, instantiate, initialize
*/
int[][] intArray =
{
{ 1, 2, 3 },
{ 4, 5, 6 } };// 2 rows and 3 columns
int rows = intArray.length;
for (int i = 0; i < rows; i++)
{
int columns = intArray[i].length;
System.out.println("Number of columns in row " + i + " is : "
+ columns);
for (int j = 0; j < columns; j++)
{
System.out.println("Element at " + "intArray[" + i + "]" + "["
+ j + "] : " + intArray[i][j]);
}
System.out.println();
}
}
}