Table of Contents
ToggleArrayList in Java with Examples
陣列列表也稱為動態陣列,可以包含重複的元素,會依新增順序保持排序,自動調整其容量,是一個非同步的操作,並且以陣列索引為基礎,允許隨機訪問, ArrayList in Java 介紹常見的 forEach 、 iterator 、 listIterator 、 spliterator 、 trySplit 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- list
| +- arraylist
| +- ArrayListWithExamplesTest.java
單元測試
ArrayList Java 提供循環訪問、保留相同元素、轉成陣列等操作列表中的元素。
loop
建立一個 ArrayList ,內有三個元素,迴圈取得元素。
@Test
public void loop() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
Apple
Banana
Cherry
forEach
建立一個 ArrayList ,內有三個元素,迴圈取得元素。
@Test
public void forEach() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.forEach(e -> System.out.println(e));
}
Apple
Banana
Cherry
forEachRemaining
ArrayList Java 建立一個 ArrayList ,內有三個元素,迴圈取得剩餘元素。
@Test
public void forEachRemaining() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Iterator<String> it = list.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
Apple
Banana
----------
Cherry
iterator
ArrayList Java 建立一個 ArrayList ,內有三個元素,迴圈取得元素。
@Test
public void iterator() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
Apple
Banana
Cherry
listIterator
ArrayList Java 建立一個 Java ArrayList ,內有三個元素,迴圈取得元素。
@Test
public void listIterator() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
ListIterator<String> it = list.listIterator(list.size());
while (it.hasPrevious()) {
System.out.println(it.previous());
}
}
Cherry
Banana
Apple
spliterator
Java ArrayList 建立一個 ArrayList ,內有三個元素,使用 spliterator 取得元素,等同於 iterator.hasNext 和 iterator.next。
@Test
public void spliterator() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Spliterator<String> sit = list.spliterator();
sit.tryAdvance(e -> System.out.println(e));
System.out.println("----------");
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
sit = list.spliterator();
while (sit.tryAdvance(e -> System.out.println(e))) {
}
}
Apple
----------
Banana
Cherry
----------
Apple
Banana
Cherry
trySplit
Java ArrayList 建立一個 ArrayList ,內有三個元素,使用 spliterator 取得元素,trySplit 將目前的拆分器分為兩個新的拆分器並行處理。
@Test
public void trySplit() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Spliterator<String> sit = list.spliterator();
Spliterator<String> sit2 = sit.trySplit();
System.out.println(sit.getExactSizeIfKnown());
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
System.out.println(sit2.getExactSizeIfKnown());
sit2.forEachRemaining(e -> System.out.println(e));
}
2
Banana
Cherry
----------
1
Apple
toArray
Java ArrayList 建立一個 ArrayList ,內有三個元素,轉換成 String 陣列。
@Test
public void toArray() {
int expectedSize = 3;
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
String[] array = new String[list.size()];
list.toArray(array);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
Apple
Banana
Cherry
streamToArray
建立一個 ArrayList ,內有三個元素,使用 Stream ,轉換成 String 陣列。
@Test
public void streamToArray() {
int expectedSize = 3;
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
String[] array = list.stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
Apple
Banana
Cherry
ArrayListWithExamplesTest.java
Java ArrayList 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.list.arraylist;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Spliterator;
import org.junit.Test;
public class ArrayListWithExamplesTest {
@Test
public void loop() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
@Test
public void forEach() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.forEach(e -> System.out.println(e));
}
@Test
public void forEachRemaining() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Iterator<String> it = list.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
@Test
public void iterator() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void listIterator() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
ListIterator<String> it = list.listIterator(list.size());
while (it.hasPrevious()) {
System.out.println(it.previous());
}
}
@Test
public void spliterator() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Spliterator<String> sit = list.spliterator();
sit.tryAdvance(e -> System.out.println(e));
System.out.println("----------");
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
sit = list.spliterator();
while (sit.tryAdvance(e -> System.out.println(e))) {
}
}
@Test
public void trySplit() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Spliterator<String> sit = list.spliterator();
Spliterator<String> sit2 = sit.trySplit();
System.out.println(sit.getExactSizeIfKnown());
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
System.out.println(sit2.getExactSizeIfKnown());
sit2.forEachRemaining(e -> System.out.println(e));
}
@Test
public void toArray() {
int expectedSize = 3;
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
String[] array = new String[list.size()];
list.toArray(array);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
@Test
public void streamToArray() {
int expectedSize = 3;
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
String[] array = list.stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
}
心得分享
Java ArrayList Example 提供了幾種 ArrayList 常見方法的操作範例,在應用上相當廣泛,熟悉 Java ArrayList 這些方法的操作,可以快速撰寫程式,降低錯誤率,再輔以單元測試驗證,建置高效穩定的服務或系統。