You are on page 1of 15

CS212: Object-Oriented Programming

Arrays In Java
ERROR-PREVENTION TIPS

The loop-continuation condition should prevent the


accessing of elements outside this range.

An exception indicates that an error has occurred in a


program. When a program attempts to access an
element outside the array index bounds, an
ArrayIndexOutOfBoundsException occurs.

2
ENHANCED FOR LOOP STATEMENT
Enhanced for statement
 Allows iteration through elements of an array or a collection
without using a counter.

 Syntax:
for (parameter : arrayName){
//statements
}
 Parameter has a type and an identifier.
 Type of the parameter must be consistent with the type of
the elements in the array

3
ENHANCED FOR LOOP STATEMENT
String[] arr = {"Alpha", "Beta", "Gamma", "Delta", "Sigma"};

for(int i = 0; i < arr.length; i++){

System.out.println(arr[i]);

for (String str: arr){

System.out.println(str);

4
Exercise:
Calculate sum of all the elements of the given
array using enhanced for loop:

int array[] = { 87, 68, 94, 100, 83, 78, 85, 91,
76, 87 };
ENHANCED FOR LOOP STATEMENT

1 // EnhancedForTest.java
2 // Using enhanced for statement to total integers in an array.
3
4 public class EnhancedForTest For each iteration, assign the next
5 { element of array to int variable
6 public static void main( String args[] ) number, then add it to total
7 {
8 int array[] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
9 int total = 0;
10
11 // add each element's value to total
12 for ( int number : array )
13 total += number;
14
15 System.out.printf( "Total of array elements: %d\n", total );
16 } // end main
17 } // end class EnhancedForTest
Total of array elements: 849
6
ENHANCED FOR LOOP STATEMENT
Lines 12-13 are equivalent to:
for (int counter = 0; counter < array.length; counter++)
total += array[ counter ];

Usage Limitations
Can traverse array elements
Cannot modify array elements
Cannot access the counter indicating the index

7
CLASS ACTIVITY EXERCISE USING ARRAYS
(CONT.)
Use arrays to analyze survey results
 40 students rate the quality of food
 1-10 Rating scale: 1 means awful, 10 means excellent
 Place 40 responses in array of integers
 Summarize results
8
Multi-Dimensional Arrays
MULTIDIMENSIONAL ARRAYS
Array of arrays
Tables with rows and columns
Two-dimensional array
m-by-n array

10
Example
Suppose there are 4 factories and each of these
factories produce items of 3 different types i.e.
bio-degradable, ,non-biodegradable and
recyclable. We have to calculate the total
products(category wise) being produced by all
factories i.e. sum of the items of each type that
all 4 factories produce.
Fact[0] Fact[1] Fac[3] Fact[4]
Bio-degradable 300 600 77 560
Non-biodegradable 200 70 390 5
Recyclable 40 89 230 8
MULTIDIMENSIONAL ARRAYS-
INDIRECT
Syntax 2D Array Creation:
data_type[][] array_name = new
data_type[size1][size2];

Example:
int[][] array_2D = new int[10][20];

Initialization Syntax:
array_name[row_index][column_index] =
value;
MULTIDIMENSIONAL ARRAYS-
DIRECT
Syntax 2D Array Direct Initialization:

data_type[][] array_name = {
{valueR1C1, valueR1C2, ....},
{valueR2C1, valueR2C2, ....}
};
Example:

int b[][] = {
{1, 2} ,
{3, 4}
};
MULTIDIMENSIONAL ARRAYS
 Declaring two-dimensional array b[2][2]
int b[][] = { { 1, 2 }, { 3, 4 } };
1 and 2 initialize b[0][0] and b[0][1]
3 and 4 initialize b[1][0] and b[1][1]

col 0 col 1
b[0][0]=1 b[0][1]=2
row 0

b[1][0]=3 b[1][1]=4
row 1

14
Questions!!

You might also like