ArrayList method summary

ArrayList method summary

The ArrayList class provides many useful methods, such as:

  • add(E e): Appends the specified element to the end of this list.
  • add(int index, E element): Inserts the specified element at the specified position in this list.
  • addAll(Collection<? extends E> c): Appends all elements in the specified collection to the end of this list in the order in which they are returned by the specified collection's iterator.
  • addAll(int index, Collection<? extends E> c): Inserts all elements in the specified collection into this list, starting at the specified position.
  • clear(): Removes all elements from this list.
  • contains(Object o): Returns true if this list contains the specified element.
  • ensureCapacity(int minCapacity): Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity parameter.
  • get(int index): Returns the element at the specified position in this list.
  • indexOf(Object o): Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
  • isEmpty(): Returns true if this list contains no elements.
  • remove(int index): Removes the element at the specified position in this list.
  • remove(Object o): Removes the first occurrence of the specified element in this list, if present.
  • set(int index, E element): Replaces the element at the specified position in this list with the specified element.
  • size(): Returns the number of elements in this list.
  • toArray(): This method is used to return an array containing all elements in this list. The returned array is "safe" in that this list keeps no references to it. (In other words, this method must allocate a new array). Therefore, the caller is free to modify the returned array.
  • toArray(T[] a): This method is used to return an array containing all the elements in this list; the runtime type of the returned array is the runtime type of the specified array. If the specified array can hold a list, it is returned in it. Otherwise, a new array is allocated for the runtime type of the specified array and the size of this list.
  • trimToSize(): This method is used to adjust the capacity of this ArrayList instance to the current size of the list. Applications can use this operation to minimize the storage of ArrayList instances.

Guess you like

Origin blog.csdn.net/weixin_43472938/article/details/131096237