ArrayList Methods - AP Computer Science A
- Identify the method that removes an element at a specified index in an ArrayList.
- remove(int index). Removes by index and returns the removed element.
- What exception does the set method throw if the index is out of range?
- IndexOutOfBoundsException. Thrown when index < 0 or >= size().
- Identify the method that removes the first occurrence of a specified element in an ArrayList.
- remove(Object o). Searches and removes by value, not index.
- What does the ArrayList get method return?
- Returns the element at the specified index. Accesses elements without modifying the ArrayList.
- What is the return type of the ArrayList remove method?
- Returns the element that was removed. Returns the element that was at the index.
- What does the ArrayList hashCode method return?
- Returns hash code value for the list. Computed from elements for hashing collections.
- Identify the method to check if an ArrayList contains a specific element.
- contains(Object o). Returns true if element exists in the list.
- Identify the method that inserts all elements of a collection into an ArrayList at a specified position.
- addAll(int index, Collection<? extends E> c). Shifts existing elements to accommodate new ones.
- What is the consequence of modifying an ArrayList while iterating over it?
- Throws ConcurrentModificationException. Prevents corruption from simultaneous modifications.
- Find the method to clear all elements from an ArrayList.
- clear(). Removes all elements, making the list empty.
- Which method in ArrayList returns an iterator over the elements?
- iterator(). Enables traversal through enhanced for loops.
- Which method returns the index of the last occurrence of an element in an ArrayList?
- lastIndexOf(Object o). Searches backwards from the end of the list.
- Which method replaces an element at a specified position in an ArrayList?
- set(int index, E element). Replaces existing element and returns old value.
- What is the purpose of the toArray method in ArrayList?
- Converts the ArrayList to an array. Creates Object array containing list elements.
- What does the ArrayList indexOf method return?
- Returns index of first occurrence of an element. Returns -1 if element is not found.
- What does the toString method return for an ArrayList?
- String representation of the list. Shows elements in bracket-separated format.
- What does the subList method in ArrayList return?
- Returns a view of a portion of the list. Creates sublist that reflects changes to original.
- What is the purpose of the ArrayList add method?
- Adds an element to the ArrayList. Inserts elements at the end or specified position.
- Which method returns a shallow copy of an ArrayList?
- clone(). Creates independent copy with same elements.
- What is the boolean return type of the remove method used with an object in ArrayList?
- True if the element is successfully removed. Returns false if element was not found.
- What does the ArrayList isEmpty method check?
- Checks if the ArrayList is empty. Returns true if size equals zero.
- Find the method to add an element at a specific position in an ArrayList.
- add(int index, E element). Inserts element and shifts existing elements right.
- Identify the method to remove all elements from an ArrayList that are in a specified collection.
- removeAll(Collection<?> c). Removes elements that exist in both collections.
- Which method returns the number of elements in an ArrayList?
- size(). Returns the current number of stored elements.
- Which method sorts the elements of an ArrayList based on a specified comparator?
- sort(Comparator<? super E> c). Orders elements according to comparison logic.
- Find the method that checks for equality between an ArrayList and another object.
- equals(Object o). Compares element-by-element for structural equality.
- Which method retains only the elements in an ArrayList that are in a specified collection?
- retainAll(Collection<?> c). Keeps only common elements between collections.
- In which scenario does the ArrayList get method throw an exception?
- Throws IndexOutOfBoundsException if index is out of range. When index is negative or >= size().
- Which method in ArrayList appends all elements in a specified collection?
- addAll(Collection<? extends E> c). Adds all elements to the end sequentially.
- Find the method that checks if two ArrayLists are equal.
- equals(Object o). Compares element-by-element for structural equality.