Professional Documents
Culture Documents
Chapter Objectives
• Learn about arrays.
• Explore how to declare and manipulate
data into arrays.
• Understand the meaning of “array index
out of bounds.”
• Become familiar with the restrictions on
array processing.
• Discover how to pass an array as a
parameter to a method.
• Discover how to manipulate data in a
two-dimensional array.
• Learn about multidimensional arrays.
array 2
What is Array?
Arrays are objects that help us organize large amounts
of information of the same type
79 87 94 82 67 98 87 81 74 Array
91 elements
scores
scores[2] = 89;
scores[n] = scores[n] + 2;
System.out.println ("Top = " + scores[5]);
Why need array?
consider the problem of writing a Java program: read 5 numbers, find the
sum, and prints them in reverse order
import java.util.*;
public class ReverseOrder
ta
the da
{
public static void main(String [] args) to h old
{ 5 va riables
d
int item0, item1, item2, item3, item4; We nee 000 data?
f1
int sum; What i
Scanner input = new Scanner(System.in);
item0 = input.nextInt();
item1 = input.nextInt();
item2 = input.nextInt();
item3 = input.nextInt();
item4 = input.nextInt();
num boolean[]flags;
flags = new boolean[20];
int arraySize;
System.out.print("Enter the size of the array: "); the value of
arraySize = input.nextInt(); arraysize used to
instantiate the
int[] list = new int[arraySize]; object list
Initialization During
Declaration
Value can be assigned into the array during declaration
The variable length contains the size of the array and can be
directly accessed in a program using array name and dot
operator.
Here list.length is 6.
Loops and Arrays
Loops can manipulate array by:
sum = 0;
for(index = 0; index < sales.length;index++)
sum = sum + sales[index];
if (sales.length != 0)
average = sum / sales.length;
else
average = 0.0;
5. Determining the largest element in the array
double[] sales = new double[10];
int index, maxIndex;
double largestSale;
maxIndex = 0;
for(index = 1; index<sales.length;index++)
if (sales[maxIndex] < sales[index])
maxIndex = index;
largestSale = sales[maxIndex];
Index Out of Bounds
• An array is in bounds if:
ERROR
Using arrays
Searching a value
num[0] 10
e.g. num[1] 20
num[2] 30
int[] num = {10,20,30,40,50,60,70,80,90,100}
num[3] 40
num[4] 50
searchValue is 60, the method will return 5 num[5] 60
num[6] 70
Return num[7] 80
Location, i num[8] 90
num[9] 100
e.g:- add a number from Array1 10 11
Calculation in arrayand Array2, store total in Array[0] Array[0]
Array3 Array[1] 20 Array[1] 22
Array[2] 30 Array[2] 33
Array[3] 40 Array[3] 44
Array[4] 50 Array[4] 55
int[ ] Array1 = {10,20,30,40,50,60,70,80,90,100};
Array[5] 60 + Array[5] 66
int[ ] Array2 = {11,22,33,44,55,66,77,88,99,110};
Array[6] 70 Array[6] 77
int[ ] Array3 = new int[10];
Array[7] 80 Array[7] 88
Array[8] 90 Array[8] 99
public static void ArraySum() Array[9] 100 Array[9] 110
{
int[] Array1 = {10,20,30,40,50,60,70,80,90,100};
int[] Array2 = {11,22,33,44,55,66,77,88,99,110};
int[] Array3 = new int[10];
int i; Array[0] 21
Array[1] 42
for (i=0; i < 10; i++)
Array[2] 63
{
Array3[i] = Array1[i] + Array2[i]; Array[3] 84
Array[4] :
System.out.println("Array3["+i+"]=“ Array[5] :
+Array3[i]); Array[6] :
}
Array[7] :
}
Array[8] :
Array[9] :
Eg- Read 10 integer numbers,
Reverse and print the numbers in
element reverse order
Enter 10 nums:
public static void ReverseOrder() 56
{ 65
67
int item[] = new int[10];
43
int i; 64
//Read integers number and store in item[i] 76
System.out.println("Enter ten integers number:"); 39
for(i = 0; i < 10; i++) 77
item[i] = input.nextInt(); 47
84
After reversing:
//Print the output in reverse order are:"); 84
System.out.println("The numbers in reverse order are:"); 47
for(i = 9; i >= 0; i--) 77
System.out.println(item[i]); 39
} 76
64
43
67
65
56
Arrays as Parameters
Arrays can be passed as parameter to methods
Formal parameter
Suppose we have the following statement
Example
class StudentInfo{
Input students String name;
information's String matric;
int age;
(name,matric, age) into }
array and print out the
output import java.util.*;
public class ArrayOfObj {
int N = 3;
StudentInfo[] student = new StudentInfo[N];
Enter Students Information
___________________________
Name : BAHARUDIN OSMAN
Matric No : S11111
Age : 30
Name : BADRUL HAZMI
Matric No : S23212
Age : 28
Name : NUR BADRINA
Matric No : S34213
Age : 27
List of students :
1. S11111 BAHARUDIN OSMAN 30
2. S23212 BADRUL HAZMI 28
3. S34213 NUR BADRINA 27
array of object
create an array of arrivalTimeEmp
array 28
Setting a time for index 49
arrivalTimeEmp[49].setTime(8, 5, 10);
Delete Object
Example
Step 1 : Identify the element to delete
Step 2 : Point the object to delete to null for (i=0; i < student.length; i++)
- if the sixth element to delete if(i==5) then
student[i] = null
student Name
Matric
IC
null
Name
Matric
IC
iii. Move up all elements (after deleted object)
iv. Point the last element to null
student student
10 11 21 45
20 22 42 34
30 33 66 21
40 44 84 32
50 55 105 13
60 66 126 21
70 77 147 33
80 88 168 22
90 99 189 123
array 32
continue
array 33
double[ ][ ] sales = new double[10][5];
array 34
Accessing Array Components
The above statement stores 25.75 into row number 5 and column
number 3; (the 6th row and the 4th column)
array 35
Sales [5][3] = 25.75;
array 36
Array Initialization During Declaration
2-Dimensional array can be initialized during declaration
Eg.
array 37
Processing 2-Dimensional Array
eg.
• Initialization
• Print
• Input data/store data into 2-Dimensional array
• Sum the data
• Find the largest element
int row;
int column;
matrix
array 39
Print
for (row = 0; row < matrix.lenth; row++)
{
for ( col = 0; col < matrix[row].length; col++)
System.out.println(matrix[row][col]);
System.out.println();
}
Read Data
for (row = 0; row < matrix.length; row++)
for (col = 0; col < matrix[row].length; col++)
matrix[row][col] = Integer.parseInt(keyboard.readLine())
array 40
Largest Element in Each Row
array 41
Multidimensional Arrays
Can define three-dimensional arrays or n-dimensional arrays (n
can be any number).
array 42
Loops to Process Multidimensional Arrays