Java ArrayList教程和示例

Java programming language provides the ArrayList which is a class of resizeable array. ArrayList is an implementation of the array structure in programming languages. ArrayList contains different types of elements in a single variable.

Java编程语言提供了ArrayList,它是一类可调整大小的数组。 ArrayList是用编程语言实现的数组结构。 ArrayList在单个变量中包含不同类型的元素。

ArrayList功能 (ArrayList Features)

ArrayList provides some features to be used different implementations.

ArrayList提供了一些可在不同实现中使用的功能。

  • ArrayList inherits the AbstractList class and implements the List interface.

    ArrayList继承AbstractList类并实现List接口。
  • ArrayList size can change with the addition and removal of the elements.

    ArrayList的大小可以随着元素的添加和删除而改变。
  • ArrayList items can be accessed randomly.

    ArrayList项可以随机访问。
  • Java ArrayList provides very similar features to the C++ Vector structure.

    Java ArrayList提供了与C ++ Vector结构非常相似的功能。

创建ArrayList (Create ArrayList)

The ArrayList class is provided by the java.util package. So in order to create ArrayList the java.util.ArrayList class should be imported. During the definition of the ArrayList, the elements or items type should be provided with the ArraList<TYPE> syntax where TYPE is the element or item type. In the following example, we will create an ArrayList which items are String type.

ArrayList类由java.util包提供。 因此,为了创建ArrayList,应导入java.util.ArrayList类。 在定义ArrayList的过程中,应使用ArraList <TYPE>语法提供元素或项目类型,其中TYPE是元素或项目类型。 在下面的示例中,我们将创建一个ArrayList,其中的项均为String类型。

// import the ArrayList class
import java.util.ArrayList; 

// Create an ArrayList object
ArrayList<String> names= new ArrayList<String>(); 

names.add("Ismail");
names.add("Ali");

In the following example we will create an Integer ArrayList where we will also add integers to this ArrayList named numbers.

在下面的示例中,我们将创建一个Integer ArrayList,还将在此ArrayList中添加名为数字的整数。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<Integer> numbers= new ArrayList<Integer>();
    names.add(5);
    names.add(1);
    names.add(-8);
  } 
}

将项目添加到ArrayList (Add Items To The ArrayList)

Items can be added into the ArrayList byusing the add() function. The element we want to add will be provided as parameter to the add() function. Keep in mind that the parameter type should be the same with the ArrayList we want to add. In the following example we will add some new String type items into the ArrayList named names.

可以使用add()函数add()项目添加到ArrayList中。 我们要添加的元素将作为参数提供给add()函数。 请记住,参数类型应与我们要添加的ArrayList相同。 在下面的示例中,我们将一些新的String类型项添加到名为name的ArrayList中。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");
    System.out.println(names);
  } 
}

访问ArrayList项目 (Access ArrayList Item)

ArrayList provides indexed positioning of the items. This means every item has an index number which starts from 0 by default. We can use the index number in order to access or get an item. In the following examples, we will get the first and second item from the ArrayList names. We will use the get()function to return given index numbered item.

ArrayList提供项目的索引位置。 这意味着每个项目都有一个默认为0的索引号。 我们可以使用索引号来访问或获取项目。 在以下示例中,我们将从ArrayList名称中获得第一项和第二项。 我们将使用get()函数返回给定索引编号的项目。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");
System.out.println(names.get(0));
System.out.println(names.get(0)); } }

更改ArrayList项目 (Change ArrayList Item)

Existing ArrayList items can be changed by using the set() function. The set() function will accept two parameters where first one is the index number of the item we want to change and second is the value we want to set. In the following example we will set the 3rd item which index number is 2 value as Ecrin.

可以使用set()函数更改现有的ArrayList项目。 set()函数将接受两个参数,其中第一个是我们要更改的项目的索引号,第二个是我们要设置的值。 在下面的示例中,我们将索引号为2的第三项设置为Ecrin。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");
names.set(2,"Ecrin"); } }

从ArrayList中删除项目 (Remove An Item From ArrayList)

Items can be removed from the ArrayList where we will use the remove() function. We will also provide the index number of the item we want to remove.

可以从ArrayList中删除项目,在这里我们将使用remove()函数。 我们还将提供要删除的商品的索引号。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//This will remove the item "Ismail" names.remove(0); } }

Alternatively if we want to remove all items, removing them with the remove() item one by one is a trivial task. We can remove all items by using the clear() function without a parameter.

或者,如果我们要删除所有项目,则使用remove()项目一个一个地删除它们是一件微不足道的任务。 我们可以使用无参数的clear()函数删除所有项。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//This will remove all items names.clear(); } }

获取ArrayList大小 (Get ArrayList Size)

ArrayList provides dynamic storage where the size of the list can change during its lifetime. We can get the size of the ArrayList by using the size() method. But keep in mind that the size provide the count of the items not the index.

ArrayList提供动态存储,列表的大小可以在其生命周期内更改。 我们可以使用size()方法获得ArrayList的size() 。 但是请记住,大小提供的是项目的数量,而不是索引。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//This will return the size of the names ArrayList int name_size = names.size(); } }

循环或遍历ArrayList (Loop or Iterate Over ArrayList)

ArrayList items can be iterated with different loop or iteration structures like for, while, etc. Below we will iterate over the ArrayList named names and print each item into the console.

可以使用不同的循环或迭代结构(例如for,while等)来迭代ArrayList项目。下面,我们将遍历名为名称的ArrayList并将每个项目打印到控制台中。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//Loop over the array list items for (int i =0; i< names.size(); i++){

//Print given item to the console
System.out.println(names.get(i));
} } }

ArrayList is an iterable or enumerable object type which means it can be loop like a foreach structure by iteration with the items. For each step, the iterated value will be set to the variable name.

ArrayList是一种可迭代或可枚举的对象类型,这意味着它可以像迭代foreach结构一样通过与项的迭代进行循环。 对于每个步骤,迭代值都将设置为变量名称。

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> names= new ArrayList<String>();
    names.add("Ismail");
    names.add("Ali");
    names.add("Ahmet");
    names.add("Elif");

//Loop over the array list items with for for (String name : names){

//Print given item to the console
System.out.println(name);
} } }

排序ArrayList项目 (Sort ArrayList Items)

By default, ArrayList items are not stored in a sorted manner. All items are stored according to the adding precedence. The Collections class provides the sort() function which can be used to sort given ArrayList. In the following example, we will sort the items in the ArrayList names.

默认情况下,ArrayList项目不以排序方式存储。 所有项目均按照添加优先级进行存储。 Collections类提供了sort()函数,该函数可用于对给定的ArrayList进行排序。 在下面的示例中,我们将对ArrayList名称中的项目进行排序。

import java.util.ArrayList;
import java.util.Collections; public class MyClass { public static void main(String[] args) { ArrayList<String> names= new ArrayList<String>(); names.add("Ismail"); names.add("Ali"); names.add("Ahmet"); names.add("Elif");

Collections.sort(names); } }

In the following example, we will sort the items that number.

在下面的示例中,我们将对该编号进行排序。

import java.util.ArrayList;
import java.util.Collections; public class MyClass { public static void main(String[] args) { ArrayList<Integer> numbers= new ArrayList<Integer>(); numbers.add(7); numbers.add(1); numbers.add(4); numbers.add(2);

Collections.sort(numbers); } }
LEARN MORE  Foreach Usage In PHP, JavaScript, Java, C#, Python Programming Languages with Examples
了解更多在PHP,JavaScript,Java,C#,Python编程语言中使用Foreach的示例

翻译自: https://www.poftut.com/java-arraylist-tutorial-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值