Fiveable
Fiveable

ArrayList

Definition

ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Analogy

Think of an ArrayList like a expandable backpack. You can keep adding items to it without worrying about running out of space, and if you need to remove something, you can easily take it out.

Related terms

List Interface: A Java interface that defines common methods for working with lists.

add(): A method used to insert elements into an ArrayList.

remove(): A method used to delete elements from an ArrayList.

"ArrayList" appears in:

Practice Questions (20+)

  • How can you determine if an ArrayList is sorted using a quick algorithm?
  • Which import statement is required to use the ArrayList class in Java?
  • When should you consider using a regular array of primitives instead of an ArrayList of wrapper objects?
  • Which statement correctly creates an ArrayList called `arrayList` that stores Integers?
  • Which of the following is a way to make an ArrayList in Java?
  • What will be the output of the following code? ``` ArrayList<Integer> list = new ArrayList<>(); list.add(5); list.add(7); list.add(9); list.add(11); list.add(13); System.out.println(list.size()); ```
  • Which method accesses the fourth element in an ArrayList?
  • What will be the output of the following code? ``` ArrayList<Integer> list = new ArrayList<>(); list.add(3); list.add(6); list.add(9); list.add(12); list.add(15); System.out.println(list.get(2)); ```
  • Which method adds an element at the beginning of an ArrayList?
  • What will be the size of the ArrayList after executing the following code? ``` ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(1, 5); list.add(3, 6); list.add(5, 7); ```
  • What will be the output of the following code? ``` ArrayList<Integer> list = new ArrayList<>(); list.add(2); list.add(4); list.add(6); list.add(8); list.add(10); list.remove(1); list.remove(3); System.out.println(list.size()); ```
  • Which method is used to remove the element at index 2 from an ArrayList?
  • What will be the output of the following code? ``` ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.set(1, 5); list.set(2, 6); list.set(3, 7); System.out.println(list.get(2)); ```
  • Which method is used to change the element at index 4 in an ArrayList?
  • What does the remove method for ArrayLists return?
  • Consider the following code snippet: ``` ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(40); numbers.remove(2); numbers.set(1, 50); int result = numbers.get(2) - numbers.get(1); System.out.println("Result: " + result); ``` What will be the output when the above code is executed?
  • Consider the following code snippet: ``` ArrayList<String> names = new ArrayList<String>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); names.add("David"); names.add(2, "Eve"); names.remove(1); names.add("Frank"); names.remove(0); System.out.println(names); ``` What is the output of the code snippet?
  • Which of the following looping structures can be used to traverse an ArrayList?
  • Which ArrayList methods do we use in the for loop implementation of traversing an ArrayList?
  • What happens if we try to access an index outside the range of an ArrayList when traversing it using a for loop?


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.