zuai-logo

What are the differences between ArrayLists and arrays?

ArrayLists: Dynamic size, use methods | Arrays: Fixed size, use bracket notation

All Flashcards

What are the differences between ArrayLists and arrays?
ArrayLists: Dynamic size, use methods | Arrays: Fixed size, use bracket notation
What are the differences between selection sort and insertion sort?
Selection Sort: Finds minimum and swaps | Insertion Sort: Inserts elements into correct position
What are the differences between using a for loop and a for-each loop to traverse an ArrayList?
For loop: Uses index, can modify | For-each loop: No index, cannot modify
What are the differences between `add(E obj)` and `add(int index, E obj)` in ArrayLists?
`add(E obj)`: Appends to the end | `add(int index, E obj)`: Inserts at a specific index
What are the differences between `get(int index)` and `set(int index, E obj)` in ArrayLists?
`get(int index)`: Retrieves element at index | `set(int index, E obj)`: Updates element at index
What are the differences between using primitive types and wrapper classes in ArrayLists?
Primitive types: Cannot be stored directly | Wrapper classes: Can be stored in ArrayLists
What are the differences between searching and sorting?
Searching: Locates a specific element | Sorting: Arranges all elements in order
What are the differences between arrays and ArrayLists in terms of memory usage?
Arrays: Allocate fixed memory | ArrayLists: Dynamically allocate memory, potentially more overhead
What are the differences between the best-case time complexity of selection sort and insertion sort?
Selection Sort: O(n^2) | Insertion Sort: O(n)
What are the differences between the worst-case time complexity of selection sort and insertion sort?
Selection Sort: O(n^2) | Insertion Sort: O(n^2)
What are the steps of adding an element at a specific index in an ArrayList?
1. Specify the index and the element. 2. Shift existing elements to the right. 3. Insert the new element at the specified index.
What are the steps of removing an element at a specific index in an ArrayList?
1. Specify the index. 2. Remove the element at that index. 3. Shift subsequent elements to the left.
What are the steps to convert an ArrayList to an array?
1. Create a new array with the same size as the ArrayList. 2. Iterate through the ArrayList. 3. Copy each element from the ArrayList to the array.
What are the steps of linear search?
1. Start at the beginning of the list. 2. Compare each element to the target. 3. If a match is found, return the index. 4. If the end is reached without a match, return -1.
What are the steps of selection sort?
1. Find the minimum element in the unsorted portion. 2. Swap it with the first element of the unsorted portion. 3. Repeat for the remaining unsorted portion.
What are the steps of insertion sort?
1. Assume the first element is sorted. 2. Pick the next element. 3. Insert it into the correct position in the sorted portion.
What are the steps for safe data handling?
1. Use encryption. 2. Collect only necessary data. 3. Implement security measures. 4. Be transparent with users.
What are the steps to create an ArrayList?
1. Import the ArrayList class. 2. Declare an ArrayList object. 3. Specify the data type using generics.
What are the steps to traverse an ArrayList using a for loop?
1. Initialize a counter variable. 2. Loop through the ArrayList using the size() method. 3. Access each element using the get() method.
What are the steps to traverse an ArrayList using a for-each loop?
1. Declare a variable of the ArrayList's data type. 2. Use the for-each loop to iterate through each element. 3. Access each element directly.
What does the following code output? ```java ArrayList<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); System.out.println(list.size()); ```
2
What does the following code output? ```java ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.set(0, 15); System.out.println(list.get(0)); ```
15
Identify the error in the following code: ```java ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); System.out.println(list.get(2)); ```
IndexOutOfBoundsException: The index 2 is out of bounds for the list of size 2.
What does the following code output? ```java ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.remove(0); System.out.println(list.size()); ```
1
What does the following code output? ```java ArrayList<Integer> list = new ArrayList<>(); list.add(5); list.add(10); list.add(5); int index = list.indexOf(5); System.out.println(index); ```
0
What does the following code output? ```java ArrayList<String> list = new ArrayList<>(); System.out.println(list.size()); ```
0
Identify the error in the following code: ```java ArrayList<int> list = new ArrayList<>(); ```
ArrayLists cannot store primitive types directly. Use the Integer wrapper class instead: ArrayList<Integer>.
What does the following code output? ```java ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.clear(); System.out.println(list.size()); ```
0
What does the following code output? ```java ArrayList<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); System.out.println(list.get(1)); ```
banana
What does the following code output? ```java ArrayList<Integer> list = new ArrayList<>(); list.add(5); list.add(10); list.remove(Integer.valueOf(5)); System.out.println(list.size()); ```
1