Developing Algorithms Using ArrayLists

Caleb Thomas
6 min read
Listen to this study note
Study Guide Overview
This study guide covers standard algorithms for ArrayLists including: modifying array values and object instance variables, finding minimum/maximum, sum, mean, and mode. It also explains how to determine if all values meet a criteria, access consecutive sequences, check for duplicates, count elements fitting a criteria, shift elements left/right, reverse an ArrayList, and convert between ArrayLists and arrays.
#Standard Algorithms
Using the traversals and methods that we have learned in the previous two topics, we can make the same algorithms that we have developed for arrays (see Topic 6.4) with slight changes. Here, we will have a snippet for each algorithm you are expected to know, with each snippet annotated for you.
#Modifying Array Values
/** Doubles each element of the ArrayList
*/
public static void doubleArray(ArrayList
#Modifying Instance Variables of Objects in an Array
/** Represents a student */ public class Student { private String name;
/** Sets the name of the Student */ public void setName(String name) { this.name = name; }
/** Other instance variables, methods, and constructors not shown
/
}
// IN ANOTHER CLASS
/* Resets all students' names
*/
public static void doubleArray(ArrayList
#Finding the Minimum and Maximum
/** Finds the maximum
*/
public static int maximum(ArrayList
/** Finds the minimum
*/
public static int minimum(ArrayList
A common mistake is initializing the maxValue and minValue to 0. - If all the values in the array are positive, it would incorrectly keep minValue at 0 (all the values are greater than 0, leaving 0 as the minimum).
- If all the values in the array are negative, it would incorrectly keep maxValue at 0 (all the values are less than 0, leaving 0 as the maximum). To counter these errors, initialize these to the first value in the array.
#Finding a Sum
/** Sums up all elements in the ArrayList
*/
public static int sum(ArrayList
#Finding a Mean
/** Finds the mean/average of the ArrayList
*/
public static int mean(ArrayList
#Finding a Mode
/** Finds the mode of an ArrayList
Prerequisite: The array must have a mode
*/
public static int mode(ArrayList
currentFrequency++; } } if (currentFrequency > mostCommonFrequency) { mostCommon = array.get(i); // replaces current mode if new most common element mostCommonFrequency = currentFrequency; } } return mostCommon; // can also be modified to return the frequency }
#Determining If All Values Have a Certain Property
/** Determines whether all values are even
*/
public static boolean isEven(ArrayList
#Accessing All Consecutive Pairs/Triplets/Sequences of Length n of Elements
/** Returns all consecutive sequences of length n in the ArrayList
*/
public static void returnAllConsecutiveSequences(ArrayList
//2 loops, one to get the starting number the other to go through the sequences System.out.print(array.get(i+j) + " "); } System.out.println(); } }
#Checking if There are Duplicate Elements
/** Checks to see if there are duplicate elements
*/
public static boolean duplicates(ArrayList
// if any element matches current element being checked, return true return true; } } } return false; // if this point reached, no duplicates found }
#Determining How Many Elements Fit a Criteria
/** Returns how many even numbers there are
*/
public static int evenFrequency(ArrayList
#Shifting Elements One Index Left
/** Shifts Elements One Index to the Left
*/
public static ArrayList
#Shifting Elements One Index Right
/** Shifts Elements One Index to the Right
*/
public static ArrayList
#Reversing an Array
/** Reverses the ArrayList
*/
public static ArrayList
// places the items in the new ArrayList in opposite order of the original newArray.add(array.get(array.size() - i - 1)); } return newArray; }
#Putting Items from an ArrayList into an Array
/** Transfers all ArrayList items into an Array
*/
public static int[] arrayListToArray(ArrayList
#Putting Items from an Array into an ArrayList
/** Transfers all Array items into an ArrayList
*/
public static ArrayList
Explore more resources

How are we doing?
Give us your feedback and let us know how we can improve