What are the general steps to add an element at a specific index in an ArrayList?
1. 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?
1. 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?
1. 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?
1. 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?
1. 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.
1. 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.
1. 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?
1. 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?
1. 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.
1. 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).
Why is it important to check the size of an ArrayList before accessing elements?
To avoid IndexOutOfBoundsException by ensuring the index is within the valid range.
What happens when you add an element to a specific index in an ArrayList?
Elements from that index onward are shifted to the right, increasing their indices.
What happens when you remove an element from a specific index in an ArrayList?
Elements to the right of the removed element are shifted to the left, decreasing their indices.
Why does the `add(E obj)` method return a boolean?
To indicate whether the addition of the element was successful.
Explain the concept of shifting elements in an ArrayList when using `add(int index, E obj)`.
Existing elements at the specified index and beyond are moved one position to the right to accommodate the new element.
Explain the concept of shifting elements in an ArrayList when using `remove(int index)`.
Existing elements to the right of the specified index are moved one position to the left to fill the gap created by the removal.
Why is it important to understand that `remove(int index)` returns the removed element?
Allows you to capture and use the value that was removed from the ArrayList.
Explain the purpose of the `set(int index, E obj)` method.
Replaces the element at the specified index with the new object, allowing modification of existing elements.
Why is it important to understand that `set(int index, E obj)` returns the previous element?
Allows you to capture and potentially use the value that was replaced.
What is the significance of ArrayLists being resizable?
They can dynamically adjust their size to accommodate more or fewer elements, unlike fixed-size arrays.
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]