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
Common Arraylist Methods:
- 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
//size() & add(object)
ArrayList<Double> numbers = new ArrayList<>();
numbers.add(1.0);
numbers.add(2.0);
numbers.add(3.0);
int size = numbers.size();
System.out.println(size);
3
//void add(index, object)
//get(index)
ArrayList<Double> numbers = new ArrayList<>();
numbers.add(1.0);
numbers.add(2.0);
numbers.add(3.0);
System.out.println(numbers.get(2));
numbers.add(2,4.0);
System.out.println(numbers.get(2));
System.out.println(numbers.get(3));
3.0
4.0
3.0
// set(index, obj)
ArrayList<Double> numbers = new ArrayList<>();
numbers.add(1.0);
numbers.add(2.0);
numbers.add(3.0);
System.out.println(numbers.get(2));
numbers.set(2,4.0);
System.out.println(numbers.get(2));
3.0
4.0
// remove(index)
ArrayList<Double> numbers = new ArrayList<>();
numbers.add(1.0);
numbers.add(2.0);
numbers.add(3.0);
System.out.println(numbers.get(2));
numbers.remove(2);
System.out.println(numbers.get(0));
System.out.println(numbers.get(1));
System.out.println(numbers.get(2));
//anybody know why we get an IndexOutofBoundsException eror?
3.0
1.0
2.0
---------------------------------------------------------------------------
java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.base/java.util.Objects.checkIndex(Objects.java:361)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at .(#38:1)
Here’s an example of a program using Arrays that finds the maximum value:
public class Main {
public static void main(String[] args) {
double[] values = {1, 2, 3, 4, 5};
double maxValue = findMax(values);
System.out.println("The maximum value is: " + maxValue);
}
private static 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;
}
}
Main.main(null);
The maximum value is: 5.0
Now, how can we modify this to use an ArrayList?
public class Main {
public static void main(String[] args) {
ArrayList<Double> values = new ArrayList<>();
values.add(1.2);
values.add(3.4);
values.add(2.6);
values.add(4.9);
values.add(0.8);
double maxValue = findMax(values);
System.out.println("The maximum value is: " + maxValue);
}
private static 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;
}
}
Main.main(null);
The maximum value is: 4.9
Homework:
(Paragraph Answer)
- What is the difference between the two examples above. Which one is better and why?
The first example uses a regular array, while the second uses an ArrayList. The big difference is that arrays have a fixed size, so you can’t add or remove elements once it’s created. ArrayLists, on the other hand, are flexible and let you easily add or remove elements.
ArrayLists are usually better because they’re easier to work with and come with built-in methods like add() and remove(). Arrays might be a bit faster and use less memory, but for most situations, ArrayLists are more convenient and make your code simpler.
(Code Answer)
- Make your own algorithm using ArrayLists that finds the sum of the elements in the ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Double> values = new ArrayList<>();
values.add(1.2);
values.add(3.4);
values.add(2.6);
values.add(4.9);
values.add(0.8);
double sum = findSum(values);
System.out.println("The sum of the elements in the ArrayList is: " + sum);
}
private static double findSum(ArrayList<Double> values) {
double sum = 0.0;
for (int i = 0; i < values.size(); i++) {
sum += values.get(i);
}
return sum;
}
}
Main.main(null);
The sum of the elements in the ArrayList is: 12.9