ArrayList<String> arrayList;
arrayList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry", "date", "elderberry"));
System.out.println(arrayList);
System.out.println(arrayList.size());
// add fig to it
arrayList.add("fig");
System.out.println(arrayList);
// add grape to index 2
arrayList.add(2, "grape");
System.out.println(arrayList);
// replace index 4 with guava
arrayList.set(4, "guava");
System.out.println(arrayList);
// print element at index 3
System.out.println(arrayList.get(3));
[apple, banana, cherry, date, elderberry]
5
[apple, banana, cherry, date, elderberry, fig]
[apple, banana, grape, cherry, date, elderberry, fig]
[apple, banana, grape, cherry, guava, elderberry, fig]
cherry
1. Which of the following are methods to interact with arrays?
-
swapConsecutive
-
variable.get
-
All of the above
Answer: All of the above
2. What happens when you run a command requesting the item arr[-1]?
-
nothing, it gets the last item
-
Your computer goes crazy and crashes
-
you get the error ArrayIndexOutOfBoundsException
Answer: you get the error ArrayIndexOutOfBoundsException
Submit You scored 2 out of 2. :))
7.4 - 7.5 Hack
import java.util.ArrayList;
import java.util.Arrays;
public static String highestAndSecondHighest(ArrayList<Integer> intList) {
int max = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int secondMin = Integer.MAX_VALUE;
for (int i = 0; i < intList.size(); i++) {
if (intList.get(i) > max) {
secondMax = max;
max = intList.get(i);
} else if (intList.get(i) > secondMax) {
secondMax = intList.get(i);
}
if (intList.get(i) < min) {
secondMin = min;
min = intList.get(i);
} else if (intList.get(i) < secondMin) {
secondMin = intList.get(i);
}
}
return "Max: " + max + ", Second Max: " + secondMax + ", Min: " + min + ", Second Min: " + secondMin;
}
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
System.out.println(intList);
System.out.println(highestAndSecondHighest(intList));
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Max: 10, Second Max: 9, Min: 1, Second Min: 2
Finish the code below so that it uses the findSum() method and it finds the sum of the numbers.
public class ArrayListHacks {
private int findSum(ArrayList<Integer> values) {
// Your code here
return 0;
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(0);
nums.add(1);
nums.add(2);
nums.add(3);
nums.add(5);
nums.add(8);
ArrayListHacks hacks = new ArrayListHacks();
hacks.findSum(nums);
}
}
ArrayListHacks.main(null);
import java.util.ArrayList;
public class ArrayListHacks {
private int findSum(ArrayList<Integer> values) {
int sum = 0;
for (int value : values) {
sum += value;
}
return sum;
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(0);
nums.add(1);
nums.add(2);
nums.add(3);
nums.add(5);
nums.add(8);
ArrayListHacks hacks = new ArrayListHacks();
int sum = hacks.findSum(nums);
System.out.println("Sum: " + sum);
}
}
ArrayListHacks.main(null);
Sum: 19
public class SelectionSort {
public static void main(String[] args) {
int[] numbers = {64, 25, 12, 22, 11}; // Example array of numbers
// Perform selection sort
for (int outerLoop = 0; outerLoop < numbers.length - 1; outerLoop++) {
// Start by assuming the current item is the smallest
int minIndex = outerLoop;
// Check the rest of the array for anything smaller
for (int inner = outerLoop + 1; inner < numbers.length; inner++) {
// If you find something smaller, update the index
if (numbers[inner] < numbers[minIndex]) {
minIndex = inner;
}
}
// If the smallest item isn’t already in the right place, swap them
if (minIndex != outerLoop) {
// Swap numbers[outerLoop] and numbers[minIndex]
int temp = numbers[outerLoop];
numbers[outerLoop] = numbers[minIndex];
numbers[minIndex] = temp;
}
}
// Print the sorted array
System.out.println("Sorted array:");
for (int num : numbers) {
System.out.print(num + " ");
}
}
}
SelectionSort.main(null);
Sorted array:
11 12 22 25 64
6th PopCorn - Hack Problem: Sort the Ducks! You have a list of ducks, and each duck has a name and weight. Your task is to sort these ducks by weight in ascending order.
Choose either Selection Sort or Insertion Sort to do the sorting.
Example: Given this list:
Duck A (4.5 kg) Duck B (2.1 kg) Duck C (5.0 kg) Duck D (1.9 kg)
After sorting, the output should be:
Duck D (1.9 kg) Duck B (2.1 kg) Duck A (4.5 kg) Duck C (5.0 kg)
class DebugDuck implements Comparable<DebugDuck> {
private String name;
private double weight;
public DebugDuck(String name, double weight) {
this.name = name;
this.weight = weight;
}
@Override
public int compareTo(DebugDuck other) {
return Double.compare(this.weight, other.weight);
}
@Override
public String toString() {
return name + " (" + weight + " kg)";
}
public static void main(String[] args) {
List<DebugDuck> ducks = new ArrayList<>();
ducks.add(new DebugDuck("Daffy", 2.5));
ducks.add(new DebugDuck("Donald", 3.0));
ducks.add(new DebugDuck("Howard", 1.8));
Collections.sort(ducks);
for (DebugDuck duck : ducks) {
System.out.println(duck);
}
}
}
DebugDuck.main(null);
Howard (1.8 kg)
Daffy (2.5 kg)
Donald (3.0 kg)
7th mini Hack: What can be used in place of the blank to ensure the users data is cleared?
ArrayList<String> userData = new ArrayList<>();
userData.add("John Doe");
userData.add("john@example.com");
// Once you're done using the data
userData.clear(); // Removes all entries
userData = _____; //
Answer:
- userData = null;
ArrayList Coding Activity Objective: Students will create, manipulate, and sort an ArrayList of integers.
ArrayList<String> arrayList;
arrayList = new ArrayList<>(Arrays.asList("pineapple", "pepperoni", "mozzerella", "sardines", "sausage"));
System.out.println(arrayList);
arrayList.set(1, "tomato sauce");
System.out.println(arrayList);
arrayList.remove(3);
System.out.println(arrayList);
if (arrayList.contains("sausage")) {
System.out.println("Sausage is in Array List");
}
else
{
System.out.println("Sausage is not in Array List");
}
for (String arrayItem : arrayList) {
System.out.println(arrayItem);
}
ArrayList<Integer> intList;
intList = new ArrayList<>(Arrays.asList(1, 131, 643, 6548, 23, 74, 12));
System.out.println(intList);
Collections.sort(intList);
System.out.println(intList);
[pineapple, pepperoni, mozzerella, sardines, sausage]
[pineapple, tomato sauce, mozzerella, sardines, sausage]
[pineapple, tomato sauce, mozzerella, sausage]
Sausage is in Array List
pineapple
tomato sauce
mozzerella
sausage
[1, 131, 643, 6548, 23, 74, 12]
[1, 12, 23, 74, 131, 643, 6548]
Activity Outline:
- Create an ArrayList:
- [x] Task: Create an ArrayList of integers and add 5 elements of your choice.
- Hint: Use the ArrayList class and the .add() method to add elements.
- Modify an Element:
- [x] Task: Change the second element (index 1) of the ArrayList to a new value (e.g., 100).
- Hint: The .set() method allows you to update an element at a specific index.
- Remove an Element:
- [x] Task: Remove the third element (index 2) from the ArrayList.
- Hint: Use the .remove() method to delete an element by its index.
- Search for an Element:
- [x] Task: Check if a specific number (e.g., 30) is in the ArrayList and print a message based on whether it is found or not.
- Hint: The .contains() method can be used to check for the presence of an element.
- Loop Through the ArrayList:
- [x] Task: Use a for loop to print each element of the ArrayList.
- Hint: You can use a traditional for loop or an enhanced for loop (for-each) to iterate through the elements.
- Sort the ArrayList:
- [x] Task: Sort the ArrayList in ascending order.
- Hint: Use Collections.sort() to sort the ArrayList.
- Print the Sorted ArrayList:
- [x] Task: Print the sorted ArrayList to see the updated order of the elements.
- Hint: Use System.out.println() to print the sorted list.
ArrayList<Object> arrayList;
ArrayList<Object> arrayList = new ArrayList<>(Arrays.asList(1, "pepperoni", 3, "sardines", "sausage"));
System.out.println(arrayList);
[1, pepperoni, 3, sardines, sausage]