Java集合与映射:深入解析与实践
立即解锁
发布时间: 2025-08-17 02:35:40 阅读量: 18 订阅数: 22 


Java编程基础与SCJP认证指南
### Java集合与映射:深入解析与实践
#### 1. 集合与队列操作
在Java编程中,集合和队列是常用的数据结构。下面通过代码示例来展示栈和队列的基本操作。
```java
import java.util.Arrays;
import java.util.ArrayDeque;
public class CollectionExample {
public static void main(String[] args) {
// 使用ArrayDeque作为栈
String[] elementArray = new String[] {"sway", "and", "twist", "stacks", "tall"};
System.out.println("Array of elements: " + Arrays.toString(elementArray));
ArrayDeque<String> stack = new ArrayDeque<String>();
for (String string : elementArray)
stack.push(string);
System.out.println("Stack before: TOP->" + stack + "<-BOTTOM");
System.out.print("Popping stack: ");
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
System.out.println("\n");
// 使用ArrayDeque作为FIFO队列
elementArray = new String[] {"Waiting", "in", "queues", "is", "boring"};
System.out.println("Array of elements: " + Arrays.toString(elementArray));
ArrayDeque<String> fifoQueue = new ArrayDeque<String>();
for (String string : elementArray)
fifoQueue.offerLast(string);
System.out.println("Queue before: HEAD->" + fifoQueue + "<-TAIL");
System.out.print("Polling queue: ");
while (!fifoQueue.isEmpty()) {
String string = fifoQueue.pollFirst();
System.out.print(string.toUpperCase() + " ");
}
System.out.println();
}
}
```
上述代码展示了如何使用`ArrayDeque`作为栈和FIFO队列。栈遵循后进先出(LIFO)原则,而队列遵循先进先出(FIFO)原则。
流程图如下:
```mermaid
graph TD;
A[开始] --> B[创建元素数组];
B --> C[使用ArrayDeque作为栈];
C --> D[将元素压入栈];
D --> E[弹出栈元素];
E --> F[使用ArrayDeque作为队列];
F --> G[将元素插入队列尾部];
G --> H[从队列头部移除元素];
H --> I[结束];
```
#### 2. 集合相关的复习问题
以下是一些关于集合的复习问题及解析:
##### 2.1 关于集合的正确陈述
问题:Which statements about collections are true? (选择两个正确答案)
选项:
- (a) Some operations on a collection may throw an UnsupportedOperationException.
- (b) Methods calling optional operations in a collection must either catch an UnsupportedOperationException or declare it in their throws clause.
- (c) A List can have duplicate elements.
- (d) An ArrayList can only accommodate a fixed number of elements.
- (e) The Collection interface contains a method named get.
答案:(a)和(c)。有些集合操作可能会抛出`UnsupportedOperationException`,`List`可以包含重复元素。
##### 2.2 程序编译和运行结果
问题:What will be the result of attempting to compile and run the following program?
```java
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.TreeSet;
public class Sets {
public static void main(String[] args) {
HashSet<Integer> set1 = new HashSet<Integer>();
addRange(set1, 1);
ArrayList<Integer> list1 = new ArrayList<Integer>();
addRange(list1, 2);
TreeSet<Integer> set2 = new TreeSet<Integer>();
addRange(set2, 3);
LinkedList<Integer> list2 = new LinkedList<Integer>();
addRange(list2, 5);
set1.removeAll(list1);
list1.addAll(set2);
list2.addAll(list1);
set1.removeAll(list2);
System.out.println(set1);
}
static void addRange(Collection<Integer> col, int step) {
for (int i = step*2; i<=25; i+=step)
col.add(i);
}
}
```
选项:
- (a) The program will fail to compile, since operations are performed on incompatible collection implementations.
- (b) The program will fail to compile, since the TreeSet referenced by set2 has not been given a Comparator to use when sorting its elements.
- (c) The program will compile without error, but will throw an UnsupportedOperationException, when run.
- (d) The program will compile without error and will print all primes below 25, when run.
答案:(d)。程序将编译无误并在运行时打印出25以下的所有质数。
##### 2.3 `Collection<E>`接口中定义的方法
问题:Which of these methods are defined in the Collection<E> interface? (选择三个正确答案)
选项:
- (a) add(E o)
- (b) retainAll(Collection<?> c)
- (c) get(int index)
- (d) iterator()
- (e) indexOf(Object o)
答案:(a)、(b)和(d)。`add(E o)`用于添加元素,`retainAll(Collection<?> c)`用于保留指定集合中的元素,`iterator()`用于获取迭代器。
##### 2.4 程序输出结果
问题:What will the following program print?
```java
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Iterate {
public static void main(String[] args) {
List<String> l = new ArrayList<String>();
l.add("A"); l.add("B"); l.add("C"); l.add("D"); l.add("E");
ListIterator<String> i = l.listIterator();
i.next(); i.next(); i.next(); i.next();
i.remove();
i.previous(); i.previous();
i.remove();
System.out.println(l);
}
}
```
选项:
- (a) It will
0
0
复制全文
相关推荐










