Академический Документы
Профессиональный Документы
Культура Документы
Array
Knowledge:
Understand the execute technique of array
Skill:
Can write application program using one and two dimensional
array
Why Array?
Consider the following..(recall)
Variables Declaration
3 TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Solution Without Array (2/6)
//get student mark
System.out.print(“Mark 1:”);
mark1 = input.nextInt();
System.out.print(“Mark 2:”);
mark2 = input.nextInt();
System.out.print(“Mark 3:”);
mark3 = input.nextInt();
System.out.print(“Mark 4:”);
mark4 = input.nextInt();
System.out.print(“Mark 5:”);
mark5 = input.nextInt();
4
Get Inputs TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Solution Without Array (3/6)
//get student mark
System.out.print(“Mark 6:”);
mark6 = input.nextInt();
System.out.print(“Mark 7:”);
mark7 = input.nextInt();
System.out.print(“Mark 8:”);
mark8 = input.nextInt();
System.out.print(“Mark 9:”);
mark9 = input.nextInt();
System.out.print(“Mark 10:”);
mark10 = input.nextInt();
[8] 72 elementn=mark[n-1];
[9] 24
12 TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Declare One Dimensional Array (1/3)
Syntax 1
DataType [ ] variableName = new DataType[arraysize
of type integer]
Example :
define an array variable called monthName of
type String with length 12
Syntax 2
DataType [ ] variableName;
variableName = new DataType[arraySize of type integer]
Example :
define an array variable called studentNum of type integer
with length indicate by user input;
int length;
int[ ] studentNum;
length = input.nextInt();
studentNum = new int[length];
Example :
define an array variable called keyCode of type integer that
contains 1,6,7,3,4,2,1 as it values;
OUTPUT:
the size of mark’s array is 4
snakeLocation.length 10
EXAMPLE 2: 1 2 3 4 5 6 7 8 9 10
int[ ] snakeLocation = {10,23,34,49,66,83,86,90,95,99};
System.out.println(“the size of array snakeLocation is”+ snakeLocation.length);
OUTPUT:
the size of snakeLocation’s array is 10
sales[2]=16.0;
sales[2]=16.0;
index sales.length
14320 4 false
true
sales
[0] 10.0
?
[1] 10.0
?
[2] 16.0
10.0
?
[3] 10.0
?
if (sales.length != 0)
average = sum / sales.length;
else
average = 0.0;
sales sum average
48.97
35.4
15.7
35.97
0 12.2425
0 15.7
1 19.7
2 0.57
25 3 13.0
TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Common Operation on Array (4)
Find sum and average of array
sum = 0;
43210 < 4 true
false
for (index = 0; index < sales.length; index++)
sum = sum + sales[index];
if (sales.length != 0) true
average = sum / sales.length; 48.97/4
else
average = 0.0;
sales sum average
48.97
35.4
15.7
35.97
0 12.2425
0 15.7
1 19.7
2 0.57
26 3 13.0
TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Common Operation on Array (5)
Determining largest element in array
maxIndex = 0;
27 3 13.0
TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Common Operation on Array (5)
Determining largest element in array
maxIndex = 0; maxIndex 10
28 3 13.0
TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Common Operation on Array (6)
Copying Arrays
//original array
int ori[] = {1,2,3,4,5,,6};
//destination array
int dest[] = {10,9,8,7,6,5,4,3,2,1};
//result
dest has the following contents 1,2,3,4,5,6,4,3,2,1
if (listA == listB)
...
listB = listA;
0 15.7 0 15.7
1 19.7 1 19.7
2 0.57 2 0.57
33 3 13.0
TMK3102 – PENGATURCARAAN KOMPUTER JUL 2006/2007
Multi Dimensional Array
Arrray of arrays
int twoDim[] = new int [4][];
twoDim[0] = new int[2];
twoDim[1] = new int[2];
twoDim[2] = new int[3];
twoDim
0 34
2 1 19
twoDim[0] 0
1 19
twoDim[1]
0 23
twoDim[2]
1 19
twoDim[3]
2 0 twoDim[1][1]
//declaration
int[] mark = new int[3];
int sum = 0;
int index ;
int average = 0;
int numberOfStudentOverAverage=0;
Scanner input = new Scanner (System.in);
//7. end
}
}