mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-12-23 14:21:29 +00:00
610 B
610 B
More Midterm Review
Let us suppose that we have the following array
int[] b = {11, 12, 15, 16, 21}
Increase all elements by 2
for (int i = 0; i < b.length; i++) {
b[i] += 2;
}
Print all elements of array
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
Sum all the elements of an array
int sum = 0;
for (int i = 0; i < b.length; i++) {
sum += b[i];
}
Access Last Element of Array
System.out.println(b[b.length - 1]);
Access the middle element
System.out.println(b[b.length / 2]);