Home | 7.1 Introduction | 7.2 Methods | 7.3 Traversing | 7.4 Algorithms | 7.5 Searching | 7.6 Sorting | 7.7 Ethical Issues |
7.4 - Developing Algorithms Using ArrayLists
ArrayLists Lesson
7.4 Developing Algorithms Using ArrayLists
Prerequisite Knowledge:
- size(): Returns the size of the arraylist as an Integer
- add(object): Adds an object to the end of your ArrayList
- void add(index, object): Addes an object to an index of your choice. Shifts the index of everything to the right by one and increases size by 1
- get(index): Retrieves the object at the index specified
- set(index, obj): Like void add, but instead of adding, it replaces the object that’s already in that index
- remove(index): Removes the object at specified index
Here’s an example of a program using Arrays that finds the maximum value:
public class ArrayListExample {
private double findMax(double[] values) {
double max = values[0];
for (int index = 1; index < values.length; index++) {
if (values[index] > max) {
max = values[index];
}
}
return max;
}
public static void main(String[] args) {
double[] nums = {1.0, 3.0, 2.0, 2.0, 1.0, 5.0, 2.421, 4.0, 61.0, 2.0, 51.0, 120.0};
ArrayListExample example = new ArrayListExample();
double max = example.findMax(nums);
System.out.println("Maximum value: " + max);
}
}
ArrayListExample.main(null);
Now, how can we modify this to use an ArrayList?
public class ArrayListExample {
private double findMax(ArrayList<Double> values) {
double max = values.get(0);
for (int index = 1; index < values.size(); index++) {
if (values.get(index) > max) {
max = values.get(index);
}
}
return max;
}
public static void main(String[] args) {
ArrayList<Double> nums = new ArrayList<>();
nums.add(41.0);
nums.add(384.0);
nums.add(2.0);
nums.add(25.0);
nums.add(11.0);
nums.add(120.0);
nums.add(21.0);
nums.add(4.03);
nums.add(6.0);
nums.add(2.230);
nums.add(25.01);
nums.add(10.420);
ArrayListExample example = new ArrayListExample();
double max = example.findMax(nums);
System.out.println("Maximum value: " + max);
}
}
ArrayListExample.main(null);
Homework:
(Paragraph Answer)
- What is the difference between the two examples above. Which one is better and why? Arrays can be more efficient than ArrayList in certain situations due to their simplicity and fixed size. Because arrays have a fixed size, they use less memory and provide faster access to elements through direct indexing, which can lead to better performance when the size of the data set is known in advance and doesn’t need to change. Additionally, arrays do not have the overhead of dynamic resizing, which can make them faster for situations where elements are added or removed frequently. Arrays are also more suitable for use in low-level programming and algorithms that require multi-dimensional structures, such as matrices. However, ArrayList provides more flexibility with its ability to grow and shrink dynamically, making it easier to use when the number of elements is unpredictable.
(Code Answer)
- Make your own algorithm using ArrayLists. Do not use the size and get methods, those are way too easy!
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(6);
numbers.add(7);
numbers.add(10);
numbers.add(13);
numbers.add(16);
for (Integer num : numbers) {
if (num % 2 == 0) {
System.out.println(num);
}
}
}
}
Main.main(null);
6
10
16