You are on page 1of 38

Array

An array is a collection (sequence) of a fixed number of variables called


elements or components, wherein all the elements are of the same data type.
A one-dimensional array is an array in which the elements are arranged in a list
form. The remainder of this section discusses one-dimensional arrays

The general form to declare a one dimensional array is:


dataType[] arrayName; //Line 1

where dataType is the element type.


Because an array is an object, arrayName is a reference variable. Therefore,
the preceding statement only declares a reference variable. Before we can
store the data, we must instantiate the array object.
The general syntax to instantiate an array object is:
arrayName = new dataType[intExp]; //Line 2
where intExp is any expression that evaluates to a positive integer. Also, the value of
intExp specifies the number of elements in the array.
You can combine the statements in Lines 1 and 2 into one statement as follows:

dataType[] arrayName = new dataType[intExp]; //Line 3

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.

Consider the following statements:


int alpha[], beta; //Line 2
int[] gamma, delta; //Line 3
The statement in Line 2 declares the variables alpha and beta. Similarly, the statement in
Line 3 declares the variables gamma and delta.
However, the statement in Line 2 declares only alpha to be an array reference variable,
while the variable beta is an int variable.
On the other hand, the statement in Line 3 declares both gamma and delta to be array
reference variables.
Accessing Array Elements
The general form (syntax) used to access an array element is:
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.
In Java, [] is an operator called the array subscripting operator.
Consider the following statement:
int[] list = new int[10];
This statement declares an array list of 10 elements.
The elements are list[0], list[1], . . ., list[9]. In other words, we have
declared 10 variables of type int
The assignment statement:
list[5] = 34;
stores 34 into list[5], which is the sixth element of the
array list
Suppose i is an int variable. Then, the assignment statement:
list[3] = 63; is equivalent to the assignment statements:
i = 3;
list[i] = 63;
If i is 4, then the assignment statement:
list[2 * i - 3] = 58;
stores 58 into list[5], because 2 * i - 3 evaluates to 5.
The index expression is evaluated first, giving the position of the element in the array.

Next, consider the following statements:


list[3] = 10;
list[6] = 35;
list[5] = list[3] + list[6];
The first statement stores 10 into list[3], the second statement stores 35 into list[6],
and the third statement adds the contents of list[3] and list[6] and stores the result
into list[5]
Accessing Java Elements
The general form (syntax)used to access an array element is

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.

In Java, [ ] is an operator called the array subscripting operator

Consider the following statement:


Int [ ] list =new int[8];
This statement declares an array list of 8 elements. The elements are list[0]…list[7]
We have declare 8 variables of type int

[0] [1] [2] [3] [4] [5] [6] [7]

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;

System.out.println("March has " + month_days[4] + " days.");


}
Multidimensional Arrays
A multidimensional array is an array of arrays.
A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For
example: int[][] a = new int[3][4];
To create a two-dimensional array, add each array within its own set of curly braces:

Example

int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };

myNumbers is now an array with two arrays as its elements.

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

int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };


int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
Two Dimensional Array

A two dimensional array is a collection of fixed number of elements arranged in rows


And columns, wherein all the elements are of the same type.

In two Dimensional Array, data is presented in a table form


[Red] [Brown] [black] [Grey]
Ford
InStock 12 21 44 52

BMW 43 51 22 2
Volvo 23 4 7 9

A syntax for declaring a two Dim Array

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

[1] 0.0 0.0 0.0

[2] 0.0 0.0 0.0

[3] 0.0 0.0 0.0

[4] 0.0 0.0 0.0


We can also use a for loop inside another for loop to get the elements of a two-dimensional array (we still
have to point to the two indexes):

Example

public class Main {


public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
}
}
Accessing an Array
To access the elements of a two-Dim array, you need a pay of indices,
one of the row position and one of the column position

The syntax to access an element of a two Dim array is

arrayName [indexExp1] [indexExp2]


Where indexExp1 and indexExp2 are expressions yielding nonnegative integer values
indexExp1 specifies the row position and indexExp2 specifies the column position.

The statement sales[5] [3] = 25.75


Store 25.75 into row number 5 and column number 3.

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

You might also like