0% found this document useful (0 votes)
20 views

Arrays

Uploaded by

Alex O'Neill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Arrays

Uploaded by

Alex O'Neill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Note – slight change from original notes

17
18
19
20
Java has well-defined rules for specifying the order in which the operators in an
expression are evaluated when the expression has several operators. For example,
multiplication and division have a higher precedence than addition and subtraction.
Precedence rules can be overridden by explicit parentheses.
Precedence Order.
When two operators share an operand the operator with the higher precedence goes
first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1
* 2) + 3 since multiplication has a higher precedence than addition.

21
22
public static void main(String[] args) {

int[] feetSize;
int total, average;
total =0;
average =0;

feetSize = new int[4];

// populate the array


feetSize[0] = 6;
feetSize[2] = 7;
feetSize[3] = 5;
feetSize[1] = 4;

// iterate through the array


for (int loop=0; loop<feetSize.length; loop++){
System.out.println(loop +" size : " +feetSize[loop]);
total+=feetSize[loop];
}
// calculate average and output to screen
average = total / feetSize.length;
System.out.println("Average is : "+average);
}

23
24
25
26
27
28

You might also like