5 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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.
/** Doubles each element of the ArrayList
*/
public static void doubleArray(ArrayList
/** 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
/** 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.
/** Sums up all elements in the ArrayList
*/
public static int sum(ArrayList
/** Finds the mean/average of the ArrayList
*/
public static int mean(ArrayList
/** 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 }
/** Determines whether all values are even
*/
public static boolean isEven(ArrayList
/** 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();
} }
/** 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 }
/** Returns how many even numbers there are
*/
public static int evenFrequency(ArrayList
/** Shifts Elements One Index to the Left
*/
public static ArrayList
/** Shifts Elements One Index to the Right
*/
public static ArrayList
/** 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; }
/** Transfers all ArrayList items into an Array
*/
public static int[] arrayListToArray(ArrayList
/** Transfers all Array items into an ArrayList
*/
public static ArrayList
The % operator, also known as the modulus operator, returns the remainder of a division operation. It is used to find the remainder when one number is divided by another.
Term 1 of 8
The % operator, also known as the modulus operator, returns the remainder of a division operation. It is used to find the remainder when one number is divided by another.
Term 1 of 8
The % operator, also known as the modulus operator, returns the remainder of a division operation. It is used to find the remainder when one number is divided by another.
Term 1 of 8
ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.
List Interface: A Java interface that defines common methods for working with lists.
add(): A method used to insert elements into an ArrayList.
remove(): A method used to delete elements from an ArrayList.
The term "get" refers to retrieving the value of a variable or an object's property.
set: This term refers to assigning or changing the value of a variable or an object's property.
return: This term is used when a method gives back or "returns" a value after performing some operations.
access modifier: An access modifier determines the visibility and accessibility of variables, methods, and classes in Java.
In the context of programming, private refers to a visibility modifier that restricts access to certain variables or methods within a class. It means that only other members of the same class can access those private elements.
Public: Refers to a visibility modifier that allows unrestricted access to variables or methods from any part of the program.
Encapsulation: The concept of bundling data and methods together within a class and controlling their access using visibility modifiers like private.
Access Modifier: A keyword used in programming languages to define the accessibility level of classes, variables, or methods.
The return keyword is used in functions/methods to send back a value to the caller. It terminates the execution of a function and passes control back to where it was called from.
Void method/function: A void method or function does not return any value. It performs some actions but doesn't send anything back.
Return type: The return type specifies what type of value will be returned by a function/method.
Method call/invocation: A method call refers to using/calling a specific method in your program, which may include passing arguments and receiving returned values.
A boolean is a data type that can only have two possible values: true or false. It is often used in programming to make decisions and control the flow of a program.
Condition: A condition is an expression that evaluates to either true or false. It is used in programming to make decisions and control the flow of code.
Boolean operators: Boolean operators are symbols such as AND, OR, and NOT that allow you to combine multiple conditions together in order to create more complex expressions.
Control flow: Control flow refers to the order in which statements are executed in a program. Booleans play a crucial role in controlling the flow of code by determining which statements should be executed based on certain conditions.