Unit 6 | 6.1 Array | 6.2 Traversing | 6.3 Enhanced for | 6.4 Algos |
Unit 6.4 Lesson
- Array:
- Index:
- Accessing:
- Updating:
- Dynamic Growth:
- Looping:
- Multi-dimensional arrays:
- Empty lockers:
- Traversing an array while performing an action
- Popcorn Hack: If an integer in the array is greater than the integer below it, print “Increase”
- HW Hack: Go through the array in reverse order and output each object
- HW Hack: Print words while skipping every other word (“Alpha”, “Gamma”, “Beta”, “Delta”)
Array:
Imagine a row of lockers, where each locker is labeled with an index, and inside each locker is a value.
Index:
The number above each locker that helps you find the exact item you’re looking for.
Accessing:
Walk up to the locker, open it, and see what’s inside.
Updating:
Replace the item in the locker with a new one.
Dynamic Growth:
New lockers can be added to the end of the row if more space is needed.
Looping:
Walking down the row, opening each locker one by one, and looking inside.
Multi-dimensional arrays:
Rows and columns of lockers in a grid, where you access items by specifying both the row and column.
Empty lockers:
Sometimes a locker is there, but it’s empty, representing null or undefined.
Traversing an array while performing an action
public class MaxMinInArray {
public static void findMaxAndMin(int[] array) {
if (array == null || array.length == 0) {
System.out.println("Array is empty");
return;
}
int max = array[0];
int min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
public static void main(String[] args) {
int[] array = {3, 5, 7, 2, 8, -1, 4, 10, 12};
findMaxAndMin(array);
}
}
MaxMinInArray.main(null)
Maximum value: 12
Minimum value: -1
Popcorn Hack: If an integer in the array is greater than the integer below it, print “Increase”
public class MaxMinInArray {
public static void findMaxAndMin(int[] array) {
if (array == null || array.length == 0) {
System.out.println("Array is empty");
return;
}
int max = array[0];
int min = array[0];
for (int i = 1; i < array.length; i++) {
// Add code here
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
public static void main(String[] args) {
int[] array = {3, 5, 7, 2, 8, -1, 4, 0, 12};
findMaxAndMin(array);
}
}
MaxMinInArray.main(null)
Maximum value: 12
Minimum value: -1
public class WordShifter {
public static void main(String[] args) {
String[] words = {"alpha", "beta", "gamma", "delta"};
int shiftWord = 2;
for (int count = 0; count < shiftWord; count++) {
String temp = words[0];
for (int index = 0; index < words.length - 1; index++) {
words[index] = words[index + 1];
}
words[words.length - 1] = temp;
}
for (String word : words) {
System.out.print(word + " ");
}
}
}
WordShifter.main(null)
gamma delta alpha beta
HW Hack: Go through the array in reverse order and output each object
Integer[] myArray = {0, 1, 2, 3, 4, 5};
for (int i=1; count < myArray.length; i++) {
//Add code here
};
HW Hack: Print words while skipping every other word (“Alpha”, “Gamma”, “Beta”, “Delta”)
public class WordShifter {
public static void main(String[] args) {
String[] words = {"alpha", "beta", "gamma", "delta"};
int shiftWord = 2;
for (int count = 0; count < shiftWord; count++) {
String temp = words[0];
for (int index = 0; index < words.length - 1; index++) {
words[index] = words[index + 1];
}
words[words.length - 1] = temp;
}
for (String word : words) {
System.out.print(word + " ");
}
}
}
WordShifter.main(null)