All Flashcards
What is an ArrayList?
A resizable array that can grow or shrink as needed.
What is the .size() method in ArrayLists?
Returns the number of elements currently in the ArrayList.
What is the .set() method in ArrayLists?
Replaces the element at the specified position in the ArrayList with the specified element.
What is the .get() method in ArrayLists?
Returns the element at the specified position in the ArrayList.
Define 'mode' in the context of an ArrayList.
The element that appears most frequently in the ArrayList.
Define 'mean' in the context of an ArrayList.
The average value of the elements in the ArrayList.
What does 'traverse' mean in the context of ArrayLists?
To iterate through each element of the ArrayList.
What is the purpose of a 'defaultName' parameter in the context of the provided code?
A string used to reset all student names in an ArrayList of Student objects.
What is the purpose of the 'add' method in ArrayLists?
Appends the specified element to the end of the ArrayList.
What is the difference between an Array and an ArrayList?
Arrays have a fixed size, while ArrayLists are resizable.
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3));
doubleArray(arr);
System.out.println(arr);
assuming doubleArray is the method provided in the notes.
[2, 4, 6]
Identify the error in the following code:
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3));
int sum = sum(arr);
System.out.println(sum / (arr.size()));
Integer division will truncate the decimal part of the mean. Should cast to double either sum or arr.size().
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 2, 3, 3, 3));
System.out.println(mode(arr));
assuming mode is the method provided in the notes.
3
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(2, 4, 6, 8));
System.out.println(isEven(arr));
assuming isEven is the method provided in the notes.
true
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
returnAllConsecutiveSequences(arr, 3);
assuming returnAllConsecutiveSequences is the method provided in the notes.
1 2 3 2 3 4 3 4 5
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 1));
System.out.println(duplicates(arr));
assuming duplicates is the method provided in the notes.
true
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
System.out.println(evenFrequency(arr));
assuming evenFrequency is the method provided in the notes.
3
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(shiftLeft(arr));
assuming shiftLeft is the method provided in the notes.
[2, 3, 4, 5, 1]
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(shiftRight(arr));
assuming shiftRight is the method provided in the notes.
[5, 1, 2, 3, 4]
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(reverse(arr));
assuming reverse is the method provided in the notes.
[5, 4, 3, 2, 1]
What is the importance of initializing maxValue and minValue correctly when finding the minimum and maximum in an ArrayList?
Initializing to 0 can lead to incorrect results if all values are positive (for minimum) or negative (for maximum).
Why is it important to check for duplicates in an ArrayList?
To ensure data integrity, uniqueness, or to avoid unintended behavior in algorithms.
Explain the concept of shifting elements in an ArrayList.
Moving each element to the left or right, potentially wrapping around the ends.
What is the significance of consecutive sequences in an ArrayList?
They represent patterns or groupings of elements that may have a specific meaning or relationship.
Why is it important to handle edge cases when developing algorithms for ArrayLists?
To ensure the algorithm works correctly for all possible inputs, including empty or unusual ArrayLists.
What is the advantage of using ArrayLists over arrays?
ArrayLists offer dynamic resizing, making them more flexible for handling varying amounts of data.
Explain the importance of preconditions when finding the mode of an ArrayList.
The algorithm assumes the ArrayList has a mode; otherwise, the result may be incorrect.
What is the purpose of using nested loops when checking for duplicates in an ArrayList?
To compare each element with every other element in the ArrayList.
What is the purpose of casting to double when calculating the mean of an ArrayList of integers?
To ensure that the result is a floating-point number and that any fractional part of the mean is preserved.
Why is it important to consider time complexity when choosing an algorithm for ArrayList manipulation?
Different algorithms have different performance characteristics, and choosing an efficient algorithm can significantly impact performance, especially for large ArrayLists.