Professional Documents
Culture Documents
We typically use statements similar to the one in Line 3 to create arrays to manipulate
data.
The statement:
int[] num = new int[5];
declares and creates the array num consisting of 5 elements. Each element is of type
int.
The elements are accessed as num[0], num[1], num[2], num[3], and num[4].
Alternate Ways to Declare an Array
Java allows you to declare arrays as follows:
int list[]; //Line 1
Here, the operator [] appears after the identifier list, not after the data type int.
You should be careful when declaring arrays as in Line 1.
arrayName[indexExp]
Where indexExp, called the index is an expression whose value is a nonnegative integer
less than the size of the array. The index value specifies the position of the element
in the array. In Java, the array index starts at 0.
List[3]=5
List[6]=0
public class MonthArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
Example
To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the
element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers:
Example
BMW 43 51 22 2
Volvo 23 4 7 9
dataType[ ] [ ] arrayName;
Where dataType is the data tape of the array elements
Because an array is an object, we must instantiate the object to allocate memory space
To store data. The general syntax to instantiate a two dimensional array object is:
arrayName = new dataType[intExp1] [ intExp2];
Where intExp1 and intExp2 are expressions yielding positive integer values.
The two expressions, intExp1 and intExp2 , specify the number of rows and the
number of columns respectively.
The preceding two statements can be combined into one statement, as follows
dataType [ ] [ ] arrayName = new dataTpe[intExp1] [intExp2 ];
For example
Double [ ] [ ] sales = new double [5] [3]
Declare two dim Array sales of 5 rows and 3 columns, wherein every element is of
type double initialised to the default 0,0
[0] [1] [2]
[0] 0.0 0.0 0.0
Example
Supposes that
Int i=5;
Int j=3;
Then the previous statement
Sales[5] [3] = 25.75
Is equivalent to
Sales[i] [j]=25.75
public static void main(String[] args) {
int i,j;
int [][] sales = new int [5][3];
sales[0][0] = 0;
sales[0][1] = 1;
sales[0][2] = 2;
sales[1][0] = 1;
sales[1][1] = 2;
sales[1][2] = 3;
sales[2][0] = 2;
sales[2][1] = 3;
sales[2][2] = 4;
sales[3][0] = 3;
sales[3][1] = 4;
sales[3][2] = 5;
sales[4][0] = 4;
sales[4][1] = 5;
sales[4][2] = 6;
for(i=0;i<sales.length;i++){
for(j=0;j<sales[i].length;j++){
System.out.print(sales[i][j] + " ");
}
System.out.println("\n");
}
System.out.println(sales[3][1]);
}
public class TwoDimArrayExample {
public static void main(String[] args) {
int[][] Matrix={ {1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{0}
};
System.out.println(Matrix[1][2]);
for(int i=1;i<3;i++){
for(int j=1;j<4;j++){
System.out.println("i =" +i+", j =" +j);
}
}
}
}
Public static void main(String [] args){
Into [][] twoDArrays={ {1, 2 ,3, 4, 5},
{4, 5, 6, 7, 3},
{7, 8, 9, 6, 1}
};
For (int i=0;i< twoDArrays.length;i++){
For (intj=0,j< twoDArrays[i].length;j++{
System.out.print(twoDArrays[i][j]+”\t”);
}
System.out.println();
}
}
}
Adding Elements in an Array
public class TwoDimArrayExample {
public static void main(String[] args) {
int[][] Matrix={ {1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{0}
};
int sum=0;
//System.out.println(Matrix[1][2]);
for(int i=0;i<Matrix.length;i++){
for(int j=0;j<Matrix[i].length;j++){
// System.out.println("i =" +i+", j =" +j);
System.out.print(Matrix[i][j]+" \t");
sum=sum+Matrix[i][j];
}
System.out.println();
}
System.out.println("The sum of elements in an array is"+sum);
}
}
Public static void main(String[] argrs){
String[] arrStr=new String[4];
arrStr[0]=“Java is awesome”;
arrStr[1]=“I love practicing programming”;
arrStr[2]=“I like the way we cooperate as students”;
arrStr[0]=“Viva Java!!!!”;
System.out.println(arrStr[0]);
System.out.println(arrStr[1]);
System.out.println(arrStr[2]);
System.out.println(arrStr[3]);
}
}
Finding Max and Minimum Values
Enhanced For-Each Loop for 2D Arrays