public static void main(String[] args) {
// 创建一个ArrayList
List<String> list = new ArrayList<>();
// 1. add(E element) - 添加元素
list.add("苹果");
list.add("香蕉");
list.add("橙子");
// 2. add(int index, E element) - 在指定索引位置插入元素
list.add(1, "芒果");
// 3. addAll(Collection<? extends E> collection) - 添加另一个集合的所有元素
List<String> fruits = new ArrayList<>();
fruits.add("草莓");
fruits.add("菠萝");
list.addAll(fruits);
// 4. boolean contains(Object o) - 检查列表是否包含指定元素
boolean contains = list.contains("橙子");
// 5. E get(int index) - 获取指定索引位置的元素
String element = list.get(3);
// 6. int indexOf(Object o) - 返回指定元素第一次出现的索引
int index = list.indexOf("香蕉");
// 7. boolean isEmpty() - 检查列表是否为空
boolean isEmpty = list.isEmpty();
// 8. int lastIndexOf(Object o) - 返回指定元素最后一次出现的索引
int lastIndex = list.lastIndexOf("香蕉");
// 9. E remove(int index) - 删除指定索引位置的元素
String removedElement = list.remove(2);
// 10. boolean remove(Object o) - 删除第一次出现的指定元素
boolean removed = list.remove("苹果");
// 11. boolean removeAll(Collection<?> collection) - 删除列表中与另一个集合相同的所有元素
List<String> removal = new ArrayList<>();
removal.add("芒果");
removal.add("橙子");
list.removeAll(removal);
// 12. E set(int index, E element) - 将指定索引位置的元素替换为新元素
list.set(0, "葡萄");
// 13. int size() - 返回列表中的元素数量
int size = list.size();
// 14. ListIterator<E> listIterator() - 返回在列表上进行双向迭代的列表迭代器
ListIterator<String> iterator = list.listIterator();
// 15. ListIterator<E> listIterator(int index) - 返回从指定索引开始进行双向迭代的列表迭代器
ListIterator<String> indexIterator = list.listIterator(2);
// 16. Object[] toArray() - 将列表转换为数组
Object[] array = list.toArray();
// 17. <T> T[] toArray(T[] array) - 将列表转换为指定类型的数组
String[] stringArray = list.toArray(new String[0]);
// 18. boolean containsAll(Collection<?> collection) - 检查列表是否包含另一个集合的所有元素
List<String> fruits2 = new ArrayList<>();
fruits2.add("苹果");
fruits2.add("香蕉");
boolean containsAll = list.containsAll(fruits2);
// 19. boolean retainAll(Collection<?> collection) - 保留列表中与另一个集合相同的元素,删除其他元素
List<String> retain = new ArrayList<>();
retain.add("葡萄");
retain.add("香蕉");
boolean retained = list.retainAll(retain);
// 20. List<E> subList(int fromIndex, int toIndex) - 返回列表的子列表,包括从fromIndex到toIndex的元素
List<String> sublist = new ArrayList<>(list.subList(0, 1));
// 21. void clear() - 清空列表中的所有元素
list.clear();
// 验证结果
System.out.println("contains: "+ contains);
System.out.println("element: "+ element);
System.out.println("index: "+ index);
System.out.println("isEmpty: "+ isEmpty);
System.out.println("removedElement: "+ removedElement);
System.out.println("removed: "+ removed);
System.out.println("lastIndex: " + lastIndex);
System.out.println("List: " + list);
System.out.println("Iterator: " + iterator);
System.out.println("Index Iterator: " + indexIterator);
System.out.println("Array: " + array);
System.out.println("String Array: " + stringArray);
System.out.println("Contains All: " + containsAll);
System.out.println("Retained: " + retained);
System.out.println("Sublist: " + sublist);
}
Java List工作中实用技巧
于 2023-06-13 17:18:18 首次发布