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)
Error caused due to index 2 having been removed earlier, 2 is out of bounds of the ArrayList.
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 difference between the two examples is that one uses ArrayLists and one uses Arrays. Arrays are faster as they are primitive to Java and do not require autoboxing.
ArrayLists are more flexible as they do not have a specific size and can be changed dyanmically. Arrays are better for memory as they are primitive.
Overall I would say that Arrays would be better in this situation as they would be faster and lower in memory usage; however, if the size of the array needs to be changed on the fly, the ArrayList method may hold its weight in that regard.
To show the difference in time between the two, their time to finish will be measured
- Make your own algorithm using ArrayLists that finds the sum of the elements in the ArrayList
ArrayList<Integer> aList = new ArrayList<Integer>();
for(int i=0;i<5;i++)
{
aList.add(i);
}
int[] intArr = {0,1,2,3,4};
int sum = 0;
long arrayListStartTime = System.nanoTime();
for(int i=0;i<aList.size();i++)
{
sum += aList.get(i);
}
System.out.println("ArrayList Time: " + (System.nanoTime() - arrayListStartTime));
sum = 0;
long arrayStartTime = System.nanoTime();
for(int i=0;i<intArr.length;i++)
{
sum += intArr[i];
}
System.out.println("Primitive Array Time: " + (System.nanoTime() - arrayStartTime));
ArrayList Time: 38065523
Primitive Array Time: 26800798
Sum of Elements in ArrayList
ArrayList<Integer> intlist = new ArrayList<Integer>();
//filling data
for(int i=0;i<10;i++)
{
intlist.add(i);
}
//algorithm
int sum = 0;
for(int element : intlist)
{
sum += element;
}
System.out.println(sum);
45