Java ArrayList 集合
ArrayList
继承自 AbstractList ,实现了 Iterable 、Collection、List 接口。
内部类 ListItr 实现了 ListIterator 接口。
ArrayList 可以使用 Iterator 和 ListIterator 两种迭代器访问元素
ArrayList 底层是数组结构,可以使用索引直接访问集合中的元素,调用 get() 和 set() 方法即可。
从 JDK 1.2 版本开始。
ArrayList 集合内元素可以重复。
特点 : 增删慢,改查快。 ArrayList 是线程不安全的 。
方法
示例
ArrayList<Integer> list = new ArrayList<>(); // 将指定的元素添加到此列表的尾部 list.add(10); list.add(20); System.out.println("add(E e) :" + list); // 将指定的元素插入此列表中的指定位置 list.add(1, 30); System.out.println("add(int index, E e) :" + list); // 将指定的Collection集合添加到此列表的尾部 ArrayList<Integer> newList = new ArrayList<>(); newList.add(1); newList.add(2); list.addAll(newList); System.out.println("addAll(Collection<? extends E> c) :" + list); // 将指定的Collection集合插入到此列表的指定位置 newList = new ArrayList<>(); newList.add(3); newList.add(4); list.addAll(1, newList); System.out.println("addAll(int index, Collection<? extends E> c) :" + list); // 判断集合列表中是否包含指定的元素 boolean result = list.contains(10); System.out.println("contains(Object o) :" + result); // 返回集合列表中指定位置的元素 System.out.println("get(int index) :" + list.get(4)); // 返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1 int index = list.indexOf(3); System.out.println("indexOf(Object o) :" + index); // 判断集合列表是否为空 System.out.println("isEmpty() :" + list.isEmpty()); // 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1 index = list.lastIndexOf(20); System.out.println("lastIndexOf(Object o) :" + index); // 移除集合列表中指定位置的元素,返回被移除的元素 Integer i = list.remove(1); System.out.println("remove(int index) :" + list + " return:" + i); // 移除此列表中首次出现的指定元素(如果存在) result = list.remove(Integer.valueOf(4)); System.out.println("remove(Object o) :" + list + " return:" + result); // 用指定的元素替代此列表中指定位置上的元素,返回被覆盖的元素 i = list.set(3, 40); System.out.println("set(int index, E e) :" + list + " return:" + i); // 获得集合列表中的元素数量 int size = list.size(); System.out.println("size() :" + size); // 将集合转成数组 Integer[] ints = list.toArray(new Integer[list.size()]); System.out.println("toArray(E[] e) :" + Arrays.toString(ints));
- 运行结果
遍历元素
for 循环
示例
ArrayList<Character> list = new ArrayList<>(); // 添加元素 for (char c = 'A'; c <= 'Z'; c++) { list.add(c); } // 遍历元素 for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + ", "); }
运行结果
先将集合转成数组,遍历数组
示例
ArrayList<Character> list = new ArrayList<>(); // 添加元素 for (char c = 'a'; c <= 'z'; c++) { list.add(c); } // 将集合转成数组 Character[] cs = list.toArray(new Character[list.size()]); // 遍历数组 for (int i = 0; i < cs.length; i++) { System.out.print(cs[i] + ", "); }
运行结果
foreach(增强for)
示例
ArrayList<Character> list = new ArrayList<>(); // 添加元素 for (char c = 'A'; c <= 'G'; c++) { list.add(c); } // 可以写Character,因自动拆箱,所以写char是可以的 for (char c : list) { System.out.print(c + ", "); }
运行结果
迭代器(Iterator)
示例
ArrayList<Character> list = new ArrayList<>(); // 添加元素 for (char c = 'H'; c <= 'N'; c++) { list.add(c); } // 获得集合的迭代器 Iterator<Character> it = list.iterator(); // 遍历 while (it.hasNext()) { char c = it.next(); System.out.print(c + ", "); }
运行结果
迭代器(ListIterator)
示例
ArrayList<Character> list = new ArrayList<>(); // 添加元素 for (char c = 'a'; c <= 'g'; c++) { list.add(c); } // 获得ListIterator迭代器 ListIterator<Character> li = list.listIterator(); // 遍历 while (li.hasNext()) { char c = li.next(); System.out.print(c + ", "); }
运行结果