unit 7 Flashcards
(23 cards)
Arraylists are _____ (mutable/immutable)
mutable
how does an arraylist dynamically change size?
stores items in underlying array, creates bigger/smaller array and replaces old array with new one under same reference
define and write a “wild card” import of the java util class
imports all classes within a package (not specified, such as with import java.util.ArrayList;)
import java.util.*;
(true/false) referencing java.util.ArrayList
is the same as referencing ArrayList as a class name
true, but only after ArrayList has been imported, either via a wild card import or a single
import java.util.ArrayList;
statement
(true/false) import statements must come before a class definition
trueeee
how do you declare an arraylist?
ArrayList<Type> name;</Type>
in ArrayList<Type> name, type is a:</Type>
type parameter
in the statement:
ArrayList mystery = new Arraylist();
what type is the arraylist set to?
object type, as in:
ArrayList<object></object>
what types of data can ArrayLists hold? (primitives/references/ both)
references only, thus, for example, int must be converted to Int to be stored in an ArrayList
true/false: due to autoboxing, in arraylists Ints and Doubles are NOT able to be treated like ints and doubles
false; they can be approximately basically sorta mostly yearh
how to create arraylist
like all other reference variables, we must call a constructor. in this case, declare an arraylist:
ArrayList<String> myList;</String>
and set it equal to
new ArrayList<String>();</String>
what does the size() method do?
returns number of items in array
what happens if you call the size of list2 after the following initialization:
ArrayList<String> nameList = new ArrayList<String>();
?</String></String>
what about with
ArrayList<String> list2 = null; ?
- you get returned a 0, there is literally no element and thus size = 0
- returned a null, methods cannot be called on null
when are sequential searches used? when are binaries?
when you have a desired component in unsorted data
when you have data sorted in some manner
types of sorting algorithms (and their definitions)
- selection:
- insertion:
- merge:
Which of the following is a reason to use an array instead of an ArrayList?
A. An array has faster access to its elements than a list does.
B. An array knows it length, but a list doesn’t know its length.
C. An ArrayList can allocate more space than it needs.
C, as every time an ArrayList fills up a new array is created that is twice as big. This can lead to extra space that is wasted.
the .add() method adds an object to the (left/right) of the index in the parenthesis
left
-11-2-9: Assume that numList has been initialized with the following Integer objects: [0, 1, 2, 3, 4]. What is the value of numList after mystery(5) executes?
private List<Integer> numList;
public void mystery(int n)
{
for (int i = 0; i < n; i++)
{
Integer obj = numList.remove(0);
numList.add(obj);
}
}
A. [4, 3, 2, 1, 0]
B. [1, 2, 3, 4, 0]
C. [0, 1, 2, 3, 4]
D. [2, 3, 4, 0, 1]
E. [4, 0, 1, 2, 3]</Integer>
c, ✔️ Each value is removed one at a time and added to the end of the list which results in the same list.
Consider the following code segment.
ArrayList<String> arrList = new ArrayList<String>();</String></String>
arrList.add(“A”);
arrList.add(“B”);
arrList.add(“C”);
arrList.add(“D”);
for (int i = 0; i < arrList.size(); i++)
{
System.out.print(arrList.remove(i));
}
What, if anything, is printed as a result of executing the code segment?
“AC”
During the first iteration of the loop, i has the value 0 and the size of arrList is 4, so the element at index 0 (“A”) is removed and printed. During the second iteration of the loop, i has the value 1 and the size of arrList is 3, so the element at index 1 (“C”) is removed and printed. During the third comparison of i and arrList.size() in the loop header, i has the value 2 and the size of arrList is 2, so the loop terminates.
Consider the following search method.
public static int search(int[] arr, int target)
{
int result = -1;
for (int j = 0; j < arr.length; j++)
{
if (arr[j] == target)
{
result = j; // Line 8
}
}
return result;
}
Which of the following describes the effect of replacing the statement in line 8 of the method with result = arr[j]; ?
The modified method will return the index of the first occurrence of target in arr.
B
The modified method will return the index of the last occurrence of target in arr.
C
The modified method will return target if target appears in arr and will return -1 otherwise.
D
The modified method will return -1 if target appears in arr and will return target otherwise.
E
The modified method will return -1 for all possible inputs.
Answer C
Correct. Since j represents an index into the array, arr[j] refers to an element of the array; so when arr[j] is equal to target, the modified method returns the value of target .
Consider the following correct implementation of the insertion sort algorithm.
public static void insertionSort(int[] elements)
{
for (int j = 1; j < elements.length; j++)
{
int temp = elements[j];
int possibleIndex = j;
while (possibleIndex > 0 && temp < elements[possibleIndex - 1])
{
elements[possibleIndex] = elements[possibleIndex - 1];
possibleIndex–; // line 10
}
elements[possibleIndex] = temp;
}
}
The following declaration and method call appear in a method in the same class as insertionSort.
int[] arr = {10, 8, 3, 4};
insertionSort(arr);
How many times is the statement possibleIndex–; in line 10 of the method executed as a result of the call to insertionSort ?
5 times
The while loop iterates once each time an array element is shifted to the right as a result of an insertion. Therefore, the statement in line 10 is executed each time an element is shifted to the right. For the given array, 10 is shifted right when 8 is inserted before it. Then 8 and 10 are each shifted right when 3 is inserted before them. Lastly, 8 and 10 are each shifted right when 4 is inserted before them. A total of five shifts occur, so the statement in line 10 is executed five times.
determine how many iterations of Binary Search logic will be required to find the stated value AND what value will be returned by the method given a return value of int: (the iteration and index)
[-7, -5, 0, 2, 4, 5, 9, 10]
a) 5
b) -3
a: 2 iterations, index 5
b: 3 iterations, -1