slug
type
status
category
summary
date
tags
password
icon

Key Points:

7.1 Introduction to ArrayList

  • ArrayList is a part of the java.util package and is a resizable array implementation of the List interface. It allows dynamic resizing as elements are added or removed.
  • ArrayList Basics:
    • Elements are indexed starting at 0.
    • Supports both indexed access (using get()) and modification (using set()).
    • Dynamic resizing means that the array grows automatically when the elements exceed the current capacity.
    • Common methods include add(), get(), size(), remove(), and clear().

Sample Code for 7.1:

7.2 ArrayList Methods

  • Common Methods:
    • add(element) – Adds an element to the end of the list.
    • get(index) – Returns the element at the specified index.
    • remove(index) – Removes the element at the specified index.
    • set(index, element) – Replaces the element at the specified index with the new element.
    • size() – Returns the number of elements in the list.
    • clear() – Removes all elements from the list.

Sample Code for 7.2:


 
 
 
In Java, an ArrayList cannot directly store primitive data types, such as int, char, double, etc. An ArrayList can only store objects, such as their wrapper classes (Integer, Character, Double, etc.). This is because ArrayList is based on generics, and primitive data types are not objects.
However, you can use the wrapper classes to store primitive data types. For example:
If you need to store a large number of primitive values, you might consider using Java’s alternatives, such as an int[] array, or using ArrayList<Integer>, where auto-boxing and unboxing will occur.
译文
在 Java 中,ArrayList 不能直接存储原始数据类型(primitive types),比如 intchardouble 等。ArrayList 只能存储对象类型(如 IntegerCharacterDouble 等包装类)。这是因为 ArrayList 是基于对象的泛型,而原始数据类型不是对象。
不过,你可以使用包装类来存储原始数据类型。例如:
如果你需要存储大量的原始类型数据,可以考虑使用 Java 提供的 ArrayList 替代品,像是 int[] 或者 ArrayList 的封装类,比如 ArrayList<Integer> 也会自动进行装箱和拆箱。
 
 
 
 

20 AP Style MCQs:

Questions:

  1. What is the default initial capacity of an ArrayList in Java?
      • a) 10
      • b) 20
      • c) 50
      • d) 100
  1. Which of the following methods can be used to add an element at a specific position in an ArrayList?
      • a) addAt(index, element)
      • b) insert(index, element)
      • c) add(index, element)
      • d) insertElement(index, element)
  1. Which of the following statements about ArrayList in Java is true?
      • a) ArrayList is a fixed-size collection.
      • b) ArrayList can hold only primitive data types.
      • c) ArrayList can dynamically resize.
      • d) ArrayList does not allow duplicates.
  1. What does the get() method of ArrayList return?
      • a) The index of the element
      • b) The element at the specified index
      • c) The number of elements in the list
      • d) A boolean value indicating if the element is in the list
  1. Which of the following statements is correct about the size() method of an ArrayList?
      • a) It returns the maximum capacity of the ArrayList.
      • b) It returns the number of elements in the ArrayList.
      • c) It returns the total memory occupied by the ArrayList.
      • d) It returns the last index of the ArrayList.
  1. Which method is used to remove an element at a specific index in an ArrayList?
      • a) delete(index)
      • b) remove(index)
      • c) erase(index)
      • d) pop(index)
  1. What happens if you try to access an index in an ArrayList that does not exist?
      • a) The program throws an IndexOutOfBoundsException.
      • b) It returns null.
      • c) It wraps the index in a loop and accesses a valid index.
      • d) The program compiles but throws an error during execution.
  1. If an ArrayList has 10 elements and you add 5 more elements, what will be the size of the list?
      • a) 5
      • b) 10
      • c) 15
      • d) 20
  1. Which method can be used to clear all elements from an ArrayList?
      • a) deleteAll()
      • b) clear()
      • c) removeAll()
      • d) empty()
  1. When you use the set() method on an ArrayList, what happens?
      • a) It adds a new element at the specified index.
      • b) It replaces the element at the specified index with the new element.
      • c) It removes the element at the specified index.
      • d) It shifts all elements in the list.
  1. Which of the following methods is not part of the ArrayList class?
      • a) add()
      • b) size()
      • c) insert()
      • d) remove()
  1. What is the index of the first element in an ArrayList?
      • a) 0
      • b) 1
      • c) -1
      • d) None of the above
  1. If you add an element to an ArrayList with an index greater than the size of the list, what happens?
      • a) It throws an error.
      • b) It adds the element at the last position.
      • c) It adds the element at the specified index.
      • d) It shifts elements to accommodate the new element.
  1. Which method would you use to find out if an ArrayList contains a certain element?
      • a) contains()
      • b) has()
      • c) find()
      • d) exists()
  1. Which of the following is a feature of ArrayList over arrays?
      • a) It has a fixed size.
      • b) It allows for efficient resizing.
      • c) It stores only primitive types.
      • d) It provides faster access times than arrays.
  1. What will the following code print?
      • a) apple
      • b) banana
      • c) 1
      • d) Error
  1. Which of the following best describes the remove() method in an ArrayList?
      • a) It removes an element based on its value.
      • b) It removes an element by shifting all other elements.
      • c) It removes an element at a specific index.
      • d) Both b and c.
  1. How would you remove the last element in an ArrayList?
      • a) list.remove(list.size())
      • b) list.remove(list.size() - 1)
      • c) list.delete(list.size() - 1)
      • d) list.removeLast()
  1. In the following code snippet, what is the expected output?
    1.  
      • a) 0
      • b) 1
      • c) 2
      • d) 3
  1. Which of the following statements about ArrayList is false?
      • a) It is a type of linked list.
      • b) It can grow dynamically.
      • c) It can store null values.
      • d) It implements the List interface.
 
 
 
 
 

Answers:
  1. a) 10
  1. c) add(index, element)
  1. c) ArrayList can dynamically resize.
  1. b) The element at the specified index
  1. b) It returns the number of elements in the ArrayList.
  1. b) remove(index)
  1. a) The program throws an IndexOutOfBoundsException.
  1. c) 15
  1. b) clear()
  1. b) It replaces the element at the specified index with the new element.
  1. c) insert()
  1. a) 0
  1. b) It adds the element at the last position.
  1. a) contains()
  1. b) It allows for efficient resizing.
  1. b) banana
  1. d) Both b and c.
  1. b) `list.remove(list.size() - 1)`
  1. c) 2
  1. a) It is a type of linked list.
 
 
 
 
 
 
 
 
CSA UNIT 6: ARRAYCSA UNIT 8: 2D Array
Loading...
现代数学启蒙
现代数学启蒙
推广现代数学🍚
最新发布
Statistics Key  Concepts and Selected Questions (*_*)
2025-2-20
CSA UNIT 7: ArrayList
2025-2-20
CSA UNIT 6:  ARRAY
2025-2-20
CSA UNIT 10: Recursion
2025-2-20
CSA UNIT 9: Inheritance
2025-2-19
CSA UNIT 8: 2D Array
2025-2-19
公告
🎉现代数学启蒙(MME:Modern Mathematics Enlightenment)欢迎您🎉
-- 感谢您的支持 ---