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 theList
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 (usingset()
). - Dynamic resizing means that the array grows automatically when the elements exceed the current capacity.
- Common methods include
add()
,get()
,size()
,remove()
, andclear()
.
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),比如 int
、char
、double
等。ArrayList
只能存储对象类型(如 Integer
、Character
、Double
等包装类)。这是因为 ArrayList
是基于对象的泛型,而原始数据类型不是对象。不过,你可以使用包装类来存储原始数据类型。例如:
如果你需要存储大量的原始类型数据,可以考虑使用 Java 提供的
ArrayList
替代品,像是 int[]
或者 ArrayList
的封装类,比如 ArrayList<Integer>
也会自动进行装箱和拆箱。20 AP Style MCQs:
Questions:
- What is the default initial capacity of an
ArrayList
in Java? - a) 10
- b) 20
- c) 50
- d) 100
- 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)
- 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.
- What does the
get()
method ofArrayList
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
- Which of the following statements is correct about the
size()
method of anArrayList
? - 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
.
- 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)
- 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.
- 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
- Which method can be used to clear all elements from an
ArrayList
? - a)
deleteAll()
- b)
clear()
- c)
removeAll()
- d)
empty()
- When you use the
set()
method on anArrayList
, 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.
- Which of the following methods is not part of the
ArrayList
class? - a)
add()
- b)
size()
- c)
insert()
- d)
remove()
- What is the index of the first element in an
ArrayList
? - a) 0
- b) 1
- c) -1
- d) None of the above
- 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.
- Which method would you use to find out if an
ArrayList
contains a certain element? - a)
contains()
- b)
has()
- c)
find()
- d)
exists()
- 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.
- What will the following code print?
- a) apple
- b) banana
- c) 1
- d) Error
- Which of the following best describes the
remove()
method in anArrayList
? - 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.
- 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()
- In the following code snippet, what is the expected output?
- a) 0
- b) 1
- c) 2
- d) 3
- 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:
- a) 10
- c)
add(index, element)
- c)
ArrayList
can dynamically resize.
- b) The element at the specified index
- b) It returns the number of elements in the
ArrayList
.
- b)
remove(index)
- a) The program throws an
IndexOutOfBoundsException
.
- c) 15
- b)
clear()
- b) It replaces the element at the specified index with the new element.
- c)
insert()
- a) 0
- b) It adds the element at the last position.
- a)
contains()
- b) It allows for efficient resizing.
- b) banana
- d) Both b and c.
- b) `list.remove(list.size() - 1)`
- c) 2
- a) It is a type of linked list.
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/arraylistreview
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章