Standard arrays in Java are fixed in the number of elements they can have. If you want to increase of decrease the elements in an array then you have to make a new array with the correct number of elements from the contents of the original array. An alternative is to use the ArrayList
class. The ArrayList
class provides the means to make dynamic arrays (i.e., their length can increase and decrease).
Java中的标准数组的固定数量是固定的。 如果要增加或减少数组中的元素,则必须使用原始数组内容中的元素数量正确的新数组。 一种替代方法是使用ArrayList
类。 ArrayList
类提供了制作动态数组的方法(即,它们的长度可以增加和减少)。
进口声明 ( Import Statement )
import java.util.ArrayList;
创建一个ArrayList ( Create an ArrayList )
An ArrayList
can be created using the simple constructor:
可以使用简单的构造函数创建ArrayList
:
ArrayList dynamicArray = new ArrayList();
This will create an ArrayList
with an initial capacity for ten elements. If a larger (or smaller) ArrayList
is required the initial capacity can be passed to the constructor. To make space for twenty elements:
这将创建一个初始容量为10个元素的ArrayList
。 如果需要更大(或更小的) ArrayList
则可以将初始容量传递给构造函数。 为20个元素腾出空间:
ArrayList dynamicArray = new ArrayList(20);
填充ArrayList ( Populating the ArrayList )
Use the add method to append a value to the ArrayList
:
使用add方法将一个值附加到ArrayList
:
dynamicArray.add(10);
dynamicArray.add(12);
dynamicArray.add(20);
Note: The ArrayList
only stores objects so although the above lines appear to add int values to ArrayList
the are automatically changed to Integer
objects as they are appended to the ArrayList
.
注意: ArrayList
仅存储对象,因此,尽管以上几行似乎向ArrayList
添加了int值,但在将它们附加到ArrayList
,它们会自动更改为Integer
对象。
A standard array can be used to populate an ArrayList
by converted it to a List collection using the Arrays.asList method and adding it to the ArrayList
using the addAll
method:
一个标准的阵列可用于填充一个ArrayList
通过使用Arrays.asList方法和将其添加到它转换为一个列表集合ArrayList
使用addAll
方法:
String[] names = {"Bob", "George", "Henry", "Declan", "Peter", "Steven"};
ArrayList dynamicStringArray = new ArrayList(20);
dynamicStringArray.addAll(Arrays.asList(names));
One thing to note about ArrayList
is the elements don't have to be of the same object type. Even though the dynamicStringArray
has been populated by String objects, it still can accept number values:
关于ArrayList
要注意的一件事是元素不必具有相同的对象类型。 即使dynamicStringArray
已由String对象填充,它仍然可以接受数字值:
dynamicStringArray.add(456);
To minimize the chance of errors it's best to specify the type of objects you want the ArrayList
to contain. This can be done at the creation stage by using generics:
为了最大程度地减少出错的机会,最好指定要ArrayList
包含的对象的类型。 这可以通过使用泛型在创建阶段完成:
ArrayList dynamicStringArray = new ArrayList(20);
Now the if we try to add an object that isn't a String
a compile-time error will be produced.
现在,如果我们尝试添加一个不是String
的对象,则会产生编译时错误。
在ArrayList中显示项目 ( Displaying the Items in an ArrayList )
To display the items in an ArrayList
the toString
method can be used:
要显示ArrayList
的项目,可以使用toString
方法:
System.out.println("Contents of the dynamicStringArray: " + dynamicStringArray.toString());
which results in:
结果是:
Contents of the dynamicStringArray: [Bob, George, Henry, Declan, Peter, Steven]
将项目插入ArrayList ( Inserting an Item into the ArrayList )
An object can be inserted anywhere into the ArrayList
index of elements by using the add method and passing the position for the insertion. To add the String "Max"
to the dynamicStringArray
at position 3:
通过使用add方法并传递插入位置,可以将对象插入元素的ArrayList
索引中的任何位置。 要将String "Max"
添加到dynamicStringArray
的位置3:
dynamicStringArray.add(3, "Max");
which results in (don't forget the index of an ArrayList
starts at 0):
结果(不要忘记ArrayList
的索引从0开始):
[Bob, George, Henry, Max, Declan, Peter, Steven]
从ArrayList中删除项目 ( Removing an Item from an ArrayList )
The remove
method can be used to remove elements from the ArrayList
. This can be done in two ways. The first is to supply the index position of the element to be removed:
remove
方法可用于从ArrayList
删除元素。 这可以通过两种方式完成。 首先是提供要删除的元素的索引位置:
dynamicStringArray.remove(2);
the String "Henry"
in postion 2 has been removed:
位置2中的String "Henry"
已被删除:
[Bob, George, Max, Declan, Peter, Steven]
The second is to supply the object to be removed. This will remove the first instance of the object. To remove "Max" from the dynamicStringArray
:
第二个是提供要除去的物体。 这将删除该对象的第一个实例 。 要从dynamicStringArray
删除“ Max”:
dynamicStringArray.remove("Max");
The String "Max"
is no longer in the ArrayList
:
String "Max"
不再位于ArrayList
:
[Bob, George, Declan, Peter, Steven]
替换ArrayList中的项目 ( Replacing an Item in an ArrayList )
Rather than removing an element and inserting a new one in its place the set
method can be used to replace an element in one go. Just pass the index of the element to be replaced and the object to replace it with. To replace "Peter" with "Paul":
set
方法可用于一次性替换元素,而不是删除元素并在其位置插入新元素。 只需传递要替换的元素和替换它的对象的索引。 将“ Peter”替换为“ Paul”:
dynamicStringArray.set(3,"Paul");
which results in:
结果是:
[Bob, George, Declan, Paul, Steven]
其他有用的方法 ( Other Useful Methods )
There are a number of useful methods to help navigate the contents of an arraylist:
有许多有用的方法可帮助导航arraylist的内容:
The number of elements contained within an
ArrayList
can be found using thesize
method:可以使用
size
方法找到ArrayList
包含的元素数:
After all our manipulations ofSystem.out.println("There are now " + dynamicStringArray.size() + " elements in the ArrayList");
dynamicStringArray
we're down to 5 elements:dynamicStringArray
进行所有操作之后,我们将归结为5个元素:There are now 5 elements in the ArrayList
The number of elements contained within an
ArrayList
can be found using thesize
method:可以使用
size
方法找到ArrayList
包含的元素数:
After all our manipulations ofSystem.out.println("There are now " + dynamicStringArray.size() + " elements in the ArrayList");
dynamicStringArray
we're down to 5 elements:dynamicStringArray
进行所有操作之后,我们将归结为5个元素:
Use the
indexOf
method to find the index position of a particular element:使用
indexOf
方法查找特定元素的索引位置:
TheSystem.out.println("The index position of George is : " + dynamicStringArray.indexOf("George"));
String "George"
is in index position 1:String "George"
在索引位置1:The index position of George is : 1
Use the
indexOf
method to find the index position of a particular element:使用
indexOf
方法查找特定元素的索引位置:
TheSystem.out.println("The index position of George is : " + dynamicStringArray.indexOf("George"));
String "George"
is in index position 1:String "George"
在索引位置1:To clear all the elements from an
ArrayList
the clear method is used:要清除
ArrayList
的所有元素,请使用clear方法:dynamicStringArray.clear();
Sometimes it can be useful to see if the
ArrayList
has any elements at all. Use theisEmpty
method:有时查看
ArrayList
是否有任何元素可能很有用。 使用isEmpty
方法:
which afterSystem.out.println("Is the dynamicStringArray empty? " + dynamicStringArray.isEmpty());
clear
method call above is now true:clear
方法调用后,现在为true:Is the dynamicStringArray empty? true
Sometimes it can be useful to see if the
ArrayList
has any elements at all. Use theisEmpty
method:有时查看
ArrayList
是否有任何元素可能很有用。 使用isEmpty
方法:
which afterSystem.out.println("Is the dynamicStringArray empty? " + dynamicStringArray.isEmpty());
clear
method call above is now true:clear
方法调用后,现在为true: