Lec 3
Lec 3
Lec 3
Lecture 3
i. Array
ii. Foreach
Arrays in Java
Working with Arrays of Elements
What are Arrays?
In programming, an array is a sequence of elements
Array of 5
0 1 2 3 4 Element’s index
elements … … … … …
Element of an array
3 4
10 -1
30 20 10 20 5 99 20 -1
20
30 99
5
Solution: Reverse an Array of Integers
a b c d e
Solution: Reverse Array of Strings
int[] numbers = { 1, 2, 3, 4, 5 };
for (int num : numbers) {
System.out.println(num + " ");
}
12345
Problem: Even and Odd Subtraction
Read an array of integers
Sum all even and odd numbers
Find the difference
Examples:
1 2 3 4 5 6 3 2 4 6 8 10 30
3 5 7 9 11 -35 2 2 2 2 2 2 12
Solution: Even and Odd Subtraction
143
Any Questions ?!