Java ArrayList
ArrayList 类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素。
ArrayList 继承了 AbstractList ,并实现了 List 接口。
//初始化
ArrayList<E> objectName =new ArrayList<>();
add() 将元素插入到指定位置的 arraylist 中
案例:objectName.add(内容);
addAll() 添加集合中的所有元素到 arraylist 中
案例:objectName.addAll(List objectName);
clear() 删除 arraylist 中的所有元素
案例:objectName.clear();
clone() 复制一份 arraylist
案例:objectName.clone();
contains() 判断元素是否在 arraylist
案例:objectName.contains(内容);
get() 通过索引值获取 arraylist 中的元素
案例:objectName.get(index);
indexOf() 返回 arraylist 中元素的索引值
案例:objectName.indexOf(内容);
removeAll() 删除存在于指定集合中的 arraylist 里的所有元素
案例:objectName.removeAll(List objectName);
remove() 删除 arraylist 里的单个元素
案例:objectName.remove(index);
objectName.remove(内容);
size() 返回 arraylist 里元素数量
案例:objectName.size();
isEmpty() 判断 arraylist 是否为空
案例:objectName.isEmpty();
subList() 截取部分 arraylist 的元素
案例:objectName.subList(int fromIndex, int toIndex);
set() 替换 arraylist 中指定索引的元素
案例:objectName.set(int index, E element);
sort() 对 arraylist 元素进行排序
案例:objectName.sort(Comparator.naturalOrder()); //升序
objectName.sort(Comparator.reverseOrder()); //降序
toArray() 将 arraylist 转换为数组
案例:objectName.toArray();
toString() 将 arraylist 转换为字符串
案例:objectName.toString();
ensureCapacity() 设置指定容量大小的 arraylist
案例:ensureCapacity(int minCapacity);
lastIndexOf() 返回指定元素在 arraylist 中最后一次出现的位置
案例:objectName.lastIndexOf(内容);
retainAll() 保留 arraylist 中在指定集合中也存在的那些元素
案例:objectName.retainAll(List objectName); //交集
containsAll() 查看 arraylist 是否包含指定集合中的所有元素
案例:objectName.containsAll(List objectName);
trimToSize() 将 arraylist 中的容量调整为数组中的元素个数
案例:objectName.trimToSize();
removeRange() 删除 arraylist 中指定索引之间存在的元素
案例:objectName.removeRange(int fromIndex, int toIndex);
replaceAll() 将给定的操作内容替换掉数组中每一个元素
案例:objectName.replaceAll(collback);
removeIf() 删除所有满足特定条件的 arraylist 元素
案例:objectName.removeIf(collback);
forEach() 遍历 arraylist 中每一个元素并执行特定操作
案例:objectName.forEach(collback);