All Flashcards
What is an ArrayList?
A resizable array implementation of the List interface.
What does 'index' mean in the context of ArrayLists?
The position of an element within the ArrayList, starting from 0.
What is a zero-indexed language?
A programming language where the first element of an array or list has an index of 0.
What is the purpose of the size() method in ArrayList?
Returns the number of elements currently in the ArrayList.
What is the purpose of the get(int index) method?
Returns the element at the specified index in the ArrayList.
What is the purpose of the add(E obj) method?
Appends the specified element to the end of the ArrayList.
What is the purpose of the add(int index, E obj) method?
Inserts the specified element at the specified position in the ArrayList.
What is the purpose of the remove(int index) method?
Removes the element at the specified position in the ArrayList and returns it.
What is the purpose of the set(int index, E obj) method?
Replaces the element at the specified position in the ArrayList with the specified element and returns the previous element.
What is an IndexOutOfBoundsException?
An exception thrown when trying to access an invalid index in an ArrayList.
What is the output of the following code?
java
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
System.out.println(list.size());
2
What is the output of the following code?
java
ArrayList<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.add(30);
System.out.println(nums.get(1));
20
What is the output of the following code?
java
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add(0, "C");
System.out.println(list);
[C, A, B]
What is the output of the following code?
java
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.remove(0);
System.out.println(list);
[2]
What is the output of the following code?
java
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.set(1, "three");
System.out.println(list);
[one, three]
Identify the error in the following code:
java
ArrayList<Integer> numbers = new ArrayList<>();
System.out.println(numbers.get(0));
IndexOutOfBoundsException because the ArrayList is empty.
What is the output of the following code?
java
ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(10);
int removed = list.remove(0);
System.out.println(removed);
5
What is the output of the following code?
java
ArrayList<String> list = new ArrayList<>();
list.add("hello");
String replaced = list.set(0, "world");
System.out.println(replaced);
hello
What is the output of the following code?
java
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(1, 4);
System.out.println(numbers);
[1, 4, 2, 3]
What is the output of the following code?
java
ArrayList<String> words = new ArrayList<>();
words.add("cat");
words.add("dog");
words.remove(1);
words.add("rat");
System.out.println(words);
[cat, rat]
What are the general steps to add an element at a specific index in an ArrayList?
- Ensure the index is within the valid range. 2. Shift existing elements from that index to the right. 3. Insert the new element at the specified index. 4. Increase the size of the ArrayList.
What are the general steps to remove an element at a specific index in an ArrayList?
- Ensure the index is within the valid range. 2. Store the element to be removed. 3. Shift elements from the right of the removed element to the left. 4. Decrease the size of the ArrayList. 5. Return the removed element.
What are the general steps to modify an element at a specific index in an ArrayList?
- Ensure the index is within the valid range. 2. Store the current element at the index. 3. Replace the current element with the new element. 4. Return the stored element.
What steps are involved in adding multiple elements to the end of an ArrayList?
- Call the
add(E obj)method for each element you want to add. 2. Each call appends the element to the end of the list. 3. The size of the ArrayList increases with each addition.
What is the process of retrieving an element from an ArrayList using its index?
- Ensure the index is within the valid range (0 to size() - 1). 2. Call the
get(int index)method with the desired index. 3. The method returns the element at that index.
Describe the steps to insert an element at the beginning of an ArrayList.
- Use the
add(int index, E obj)method with index 0. 2. All existing elements are shifted to the right. 3. The new element is placed at the beginning.
Describe the process of removing all occurrences of a specific value from an ArrayList.
- Iterate through the ArrayList. 2. For each element, check if it matches the value to be removed. 3. If it matches, use
remove(int index)to remove it. 4. Adjust the index accordingly due to shifting of elements.
What is the process to update multiple elements in an ArrayList based on a certain condition?
- Iterate through the ArrayList. 2. Check if each element satisfies the condition. 3. If it does, use
set(int index, E obj)to update the element.
What are the steps to create and populate an ArrayList with initial values?
- Declare and initialize an ArrayList using the constructor. 2. Use the
add(E obj)method to add each initial value to the end of the list.
Describe the steps to find the index of a specific element in an ArrayList.
- Iterate through the ArrayList. 2. For each element, check if it matches the target element. 3. If it matches, return the current index. 4. If no match is found, return -1 (or another indicator of absence).