zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What is an ArrayList?

A resizable array implementation of the List interface.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

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]

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.