Java集合框架ArrayList

This class provides implementation of an array based data structure that is used to store elements in linear order. This class implements List interface and an abstract AbstractList class. It creates a dynamic array that grows based on the elements strength.

此类提供基于数组的数据结构的实现,该数据结构用于按线性顺序存储元素。 此类实现List接口和抽象AbstractList类。 它创建一个动态数组,该数组根据元素的强度而增长。

Java ArrayList类声明 (Java ArrayList class Declaration)

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
  1. ArrayList supports dynamic array that can grow as needed.

    ArrayList支持可以根据需要增长的动态数组。

  2. It can contain Duplicate elements and it also maintains the insertion order.

    它可以包含Duplicate元素,并且还可以保持插入顺序。

  3. Manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

    操作很慢,因为如果将任何元素从数组列表中删除,则需要进行很多移位。

  4. ArrayLists are not synchronized.

    ArrayList不同步。

  5. ArrayList allows random access because it works on the index basis.

    ArrayList允许随机访问,因为它基于索引工作。

ArrayList构造函数 (ArrayList Constructors)

ArrayList class has three constructors that can be used to create Arraylist either from empty or elements of other collection.

ArrayList类具有三个构造函数,可用于从空集合或其他集合的元素创建Arraylist。

ArrayList()  // It creates an empty ArrayList
ArrayList( Collection C ) // It creates an ArrayList that is initialized with elements of the Collection C
ArrayList( int capacity ) // It creates an ArrayList that has the specified initial capacity

示例:创建一个ArrayList (Example: Creating an ArrayList)

Lets create an ArrayList to store string elements. Here list is empty because we did not add elements to it.

让我们创建一个ArrayList来存储字符串元素。 此处的列表为空,因为我们未向其中添加元素。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {   
    // Creating an ArrayList
    ArrayList< String> fruits = new ArrayList< String>();
    
    // Displaying Arraylist
    System.out.println(fruits);
  }
}

[]

[]

ArrayList方法 (ArrayList Methods)

The below table contains methods of Arraylist. We can use them to manipulate its elements.

下表包含Arraylist的方法。 我们可以使用它们来操纵其元素。

MethodDescription
void add(int index, E element)It inserts the specified element at the specified position in a list.
boolean add(E e)It appends the specified element at the end of a list.
boolean addAll(Collection<? extends E> c)It appends all of the elements in the specified collection to the end of this list.
boolean addAll(int index, Collection<? extends E> c)It appends all the elements in the specified collection, starting at the specified position of the list.
void clear()It removes all of the elements from this list.
void ensureCapacity(int requiredCapacity)It enhances the capacity of an ArrayList instance.
E get(int index)It fetches the element from the particular position of the list.
boolean isEmpty()It returns true if the list is empty, otherwise false.
int lastIndexOf(Object o)It returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
Object[] toArray()It returns an array containing all of the elements in this list in the correct order.
<T> T[] toArray(T[] a)It returns an array containing all of the elements in this list in the correct order.
Object clone()It returns a shallow copy of an ArrayList.
boolean contains(Object o)It returns true if the list contains the specified element
int indexOf(Object o)It returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
E remove(int index)It removes the element present at the specified position in the list.
boolean remove(Object o)It removes the first occurrence of the specified element.
boolean removeAll(Collection<?> c)It removes all the elements from the list.
boolean removeIf(Predicate<? super E> filter)It removes all the elements from the list that satisfies the given predicate.
protected void removeRange(int fromIndex, int toIndex)It removes all the elements lies within the given range.
void replaceAll(UnaryOperator<E> operator)It replaces all the elements from the list with the specified element.
void trimToSize()It trims the capacity of this ArrayList instance to be the list's current size.
方法 描述
void add(int index,E元素) 它将指定的元素插入列表中的指定位置。
布尔值add(E e) 它将指定的元素追加到列表的末尾。
布尔addAll(Collection <?扩展E> c) 它将指定集合中的所有元素附加到此列表的末尾。
布尔addAll(int索引,Collection <?扩展E> c) 从列表的指定位置开始,它将附加指定集合中的所有元素。
无效clear() 它将从此列表中删除所有元素。
void sureCapacity(int requiredCapacity) 它增强了ArrayList实例的容量。
E get(int索引) 它从列表的特定位置获取元素。
布尔isEmpty() 如果列表为空,则返回true,否则返回false。
int lastIndexOf(Object o) 它返回该列表中最后一次出现的指定元素的索引;如果列表不包含此元素,则返回-1。
Object [] toArray() 它以正确的顺序返回一个包含此列表中所有元素的数组。
<T> T [] toArray(T [] a) 它以正确的顺序返回一个包含此列表中所有元素的数组。
对象clone() 它返回ArrayList的浅表副本。
布尔contains(Object o) 如果列表包含指定的元素,则返回true
int indexOf(Object o) 它返回指定元素首次出现在此列表中的索引,如果列表不包含此元素,则返回-1。
E remove(int索引) 它删除存在于列表中指定位置的元素。
布尔值remove(Object o) 它删除指定元素的第一次出现。
布尔值removeAll(Collection <?> c) 它从列表中删除所有元素。
布尔值removeIf(Predicate <?super E>过滤器) 它从满足给定谓词的列表中删除所有元素。
受保护的void removeRange(int fromIndex,int toIndex) 它会删除位于给定范围内的所有元素。
void replaceAll(UnaryOperator <E>运算符) 它将列表中的所有元素替换为指定的元素。
void trimToSize() 它将此ArrayList实例的容量调整为列表的当前大小。

将元素添加到ArrayList (Add Elements to ArrayList)

To add elements into ArrayList, we are using add() method. It adds elements into the list in the insertion order.

要将元素添加到ArrayList中,我们使用add()方法。 它将按插入顺序将元素添加到列表中。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {   
    // Creating an ArrayList
    ArrayList< String> fruits = new ArrayList< String>();
    // Adding elements to ArrayList
    fruits.add("Mango");
    fruits.add("Apple");
    fruits.add("Berry");
    // Displaying ArrayList
    System.out.println(fruits);
  }
}

[Mango, Apple, Berry]

[芒果,苹果,浆果]

移除元素 (Removing Elements)

To remove elements from the list, we are using remove method that remove the specified elements. We can also pass index value to remove the elements of it.

要从列表中删除元素,我们使用remove方法删除指定的元素。 我们还可以传递索引值以删除其中的元素。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {   
    // Creating an ArrayList
    ArrayList< String> fruits = new ArrayList< String>();
    // Adding elements to ArrayList
    fruits.add("Mango");
    fruits.add("Apple");
    fruits.add("Berry");
    fruits.add("Orange");
    // Displaying ArrayList
    System.out.println(fruits);
    // Removing Elements
    fruits.remove("Apple");
    System.out.println("After Deleting Elements: \n"+fruits);
    // Removing Second Element
    fruits.remove(1);
    System.out.println("After Deleting Elements: \n"+fruits);
    
  }
}

[Mango, Apple, Berry, Orange] After Deleting Elements: [Mango, Berry, Orange] After Deleting Elements: [Mango, Orange]

[Mango,Apple,Berry,Orange]删除元素后:[Mango,Berry,Orange]删除元素后:[Mango,Orange]

ArrayList的遍历元素 (Traversing Elements of ArrayList)

Since ArrayList is a collection then we can use loop to iterate its elements. In this example we are traversing elements. See the below example.

由于ArrayList是一个集合,因此我们可以使用循环来迭代其元素。 在此示例中,我们正在遍历元素。 请参见以下示例。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {   
    // Creating an ArrayList
    ArrayList< String> fruits = new ArrayList< String>();
    // Adding elements to ArrayList
    fruits.add("Mango");
    fruits.add("Apple");
    fruits.add("Berry");
    fruits.add("Orange");
    // Traversing ArrayList
    for(String element : fruits) {
      System.out.println(element);    
    }
  }
}

Mango Apple Berry Orange

芒果苹果浆果橙

获取ArrayList的大小 (Get size of ArrayList)

Sometimes we want to know number of elements an ArrayList holds. In that case we use size() then returns size of ArrayList which is equal to number of elements present in the list.

有时我们想知道ArrayList包含的元素数量。 在这种情况下,我们使用size()然后返回ArrayList的大小,该大小等于列表中存在的元素数。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {   
    // Creating an ArrayList
    ArrayList< String> fruits = new ArrayList< String>();
    // Adding elements to ArrayList
    fruits.add("Mango");
    fruits.add("Apple");
    fruits.add("Berry");
    fruits.add("Orange");
    // Traversing ArrayList
    for(String element : fruits) {
      System.out.println(element);    
    }
    System.out.println("Total Elements: "+fruits.size());
  }
}

Mango Apple Berry Orange Total Elements: 4

芒果苹果莓果橙总元素:4

排序ArrayList元素 (Sorting ArrayList Elements)

To sort elements of an ArrayList, Java provides a class Collections that includes a static method sort(). In this example, we are using sort method to sort the elements.

为了对ArrayList的元素进行排序,Java提供了一个Collections类,其中包括一个静态方法sort()。 在此示例中,我们使用sort方法对元素进行排序。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {   
    // Creating an ArrayList
    ArrayList< String> fruits = new ArrayList< String>();
    // Adding elements to ArrayList
    fruits.add("Mango");
    fruits.add("Apple");
    fruits.add("Berry");
    fruits.add("Orange");
    // Sorting elements
    Collections.sort(fruits);
    // Traversing ArrayList
    for(String element : fruits) {
      System.out.println(element);    
    }
  }
}

Apple Berry Mango Orange

苹果浆果芒果橙

翻译自: https://www.studytonight.com/java/arraylist-in-collection-framework.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值