【个人笔记】How to use ArrayList

本文详细介绍了Java中ArrayList类的使用方法,包括构造函数、常用方法如添加、删除元素等,并对比了ArrayList与简单数组的适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this example we will show how to use ArrayList in Java. The class java.util.ArrayList provides resizable-array, which means that items can be added and removed from the list. It implements the List interface.

A major question related to arraylists is about when to use arraylists instead of arrays and vice versa. An ArrayList is a dynamic data structure so it can be used when there is no upper bound on the number of elements.

From the other side, a simple Array in java is a static data structure, because the initial size of array cannot be changed, so it can be used only when the data has a known number of elements.

1.ArrayList Constructors

The ArrayList class supports three constructors.

  • Arraylist()

This constructor builds an empty list.

  • ArrayList(Collection<? extends E> c)

This constructor creates a list containing the elements of the specified collection. Note that E is the notation for the type of an element in a collection.

  • ArrayList(int initialCapacity)

This constructor creates an empty list with the specified initial capacity.

For example, if you want to create an empty array list of Strings then you would do the following:

ArrayList<String> list = new ArrayList<String>();

If you want to create an array list with initial capacity, then you should do the following:

ArrayList<Integer> list = new ArrayList<Integer>(7);

Note that ArrayList class supports only object types and not primitive types.

2. ArrayList common methods

Here are some of the most useful ArrayList methods.

  • Adding elements to the list
    • boolean add(Element e)
      Adds the specified element to the end of this list.
    • void add(int index, Element e)
      Adds the specified element at the specified position in the list.
  • Removing elements from the list
    • void clear()
      Removes all the elements from the list.
    • E remove(int index)
      Removes the element at the specified position in the list.
    • protected void removeRange(int start, int end)
      Removes from the list all the elements starting from index start (included) until index end (not included).
  • Getting elements from the list
    • E get(int index)
      Returns the element at the specified position.
    • Object[] toArray()
      Returns an array containing all the elements of the list in proper sequence.
  • Setting an element
    • E set(int index, E element)
      Replaces the element at the specified position with the specified element.
  • Searching elements
    • boolean contains(Object o)
      Returns true if the specified element is found in the list.
    • int indexOf(Object o)
      Returns the index of the first occurrence of the specified element in the list. If this element is not in the list, the method returns -1.
    • int lastIndexOf(Object o)
      Returns the index of the last occurrence of the specified element in the list. If this element is not in the list, the method returns -1.
  • Iterating the arraylist
    • Iterator iterator()
      Returns an iterator over the elements in the list.
    • ListIterator listIterator()
      Returns a list iterator over the elements in this list.
  • Checking if the list is empty
    • boolean isEmpty()
      Returns true if the list does not contain any element.
  • Getting the size of the list
    • int size()
      Returns the length of the list (the number of elements contained in the list).

Those were the most commonly used methods of java.util.ArrayList. For further details for each method or for other methods that are not mentioned in this section, you can have a look at official java api.

原文链接 : 点击打开链接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值