arrayList.add(); —————— 加入一个元素(Object)java
arrayLIst.addAll(); —————— 加入集合元素(Collection)web
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link Collection#add}) */ public boolean add(E e) { modCount++; add(e, elementData, size); return true; }
arrayList.add(index, element);—————在指定位置添加元素>数组
public void add(int index, E element) { rangeCheckForAdd(index); modCount++; final int s; Object[] elementData; if ((s = size) == (elementData = this.elementData).length) elementData = grow(); System.arraycopy(elementData, index, elementData, index + 1, s - index); elementData[index] = element; size = s + 1; }
arrayList.remove();app
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { Objects.checkIndex(index, size); final Object[] es = elementData; @SuppressWarnings("unchecked") E oldValue = (E) es[index]; fastRemove(es, index); return oldValue; }
arrayList.set(index, element);svg
public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.set(lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }
arrayList.size();函数
arrayLIst.indexOf(); —————— 从前日后查找源码分析
public int indexOf(Object o) { int index = root.indexOfRange(o, offset, offset + size); checkForComodification(); return index >= 0 ? index - offset : -1; }
arrayList.lastIndexOf(); —————— 从后向前查找测试
public int lastIndexOf(Object o) { int index = root.lastIndexOfRange(o, offset, offset + size); checkForComodification(); return index >= 0 ? index - offset : -1; }
arrayList.contains();this
public boolean contains(Object o) { return indexOf(o) >= 0; }
arrayList.subList(1, 3);spa
public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList<>(this, fromIndex, toIndex); } ******************************************************************************* static void subListRangeCheck(int fromIndex, int toIndex, int size) { if (fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); if (toIndex > size) throw new IndexOutOfBoundsException("toIndex = " + toIndex); if (fromIndex > toIndex) throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); }
for(int i = 0; i < arrayList.size(); i++) { System.out.println(arrayList.get(i)); }
for(Object o:arrayList) { System.out.println(o); }
Iterator iterator = arrayList.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); }
遍历ArrayList时,使用随机访问 (即,经过索引序号访问 ) 效率最高,而使用迭代器的效率最低!
import java.util.*; /* * @desc ArrayList经常使用API的测试程序 * @author 端脑 */ public class ArrayListTest { public static void main(String[] args) { // 建立ArrayList ArrayList list = new ArrayList(); // 将“” list.add("1"); list.add("2"); list.add("3"); list.add("4"); // 将下面的元素添加到第1个位置 list.add(0, "5"); // 获取第1个元素 System.out.println("the first element is: "+ list.get(0)); // 删除“3” list.remove("3"); // 获取ArrayList的大小 System.out.println("Arraylist size=: "+ list.size()); // 判断list中是否包含"3" System.out.println("ArrayList contains 3 is: "+ list.contains(3)); // 设置第2个元素为10 list.set(1, "10"); // 经过Iterator遍历ArrayList for(Iterator iter = list.iterator(); iter.hasNext(); ) { System.out.println("next is: "+ iter.next()); } // 将ArrayList转换为数组 String[] arr = (String[])list.toArray(new String[0]); for (String str:arr) System.out.println("str: "+ str); // 清空ArrayList list.clear(); // 判断ArrayList是否为空 System.out.println("ArrayList is empty: "+ list.isEmpty()); } }