首页 > 未分类 > ArrayList动态数组
2017
07-16

ArrayList动态数组

Name

ArrayList动态数组

Examples

// These are code fragments that show how to use an ArrayList.

// They won’t compile because they assume the existence of a Particle class.

 

// Declaring the ArrayList, note the use of the syntax “<Particle>” to indicate

// our intention to fill this ArrayList with Particle objects

ArrayList<Particle> particles = new ArrayList<Particle>();

 

// Objects can be added to an ArrayList with add()

particles.add(new Particle());

 

// Particles can be pulled out of an ArrayList with get()

Particle part = particles.get(0);

part.display();

 

// The size() method returns the current number of items in the list

int total = particles.size();

println(“The total number of particles is: ” + total);

 

// You can iterate over an ArrayList in two ways.

// The first is by counting through the elements:

for (int i = 0; i < particles.size(); i++) {

Particle part = particles.get(i);

part.display();

}

 

// The second is using an enhanced loop:

for (Particle part : particles) {

part.display();

}

 

// You can delete particles from an ArrayList with remove()

particles.remove(0);

println(particles.size()); // Now one less!

 

// If you are modifying an ArrayList during the loop,

// then you cannot use the enhanced loop syntax.

// In addition, when deleting in order to hit all elements,

// you should loop through it backwards, as shown here:

for (int i = particles.size() – 1; i >= 0; i–) {

Particle part = particles.get(i);

if (part.finished()) {

particles.remove(i);

}

}

Description

描述

An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is resized dynamically. This can be very convenient, but it’s slower than making an array of objects when using many elements. Note that for resizable lists of integers, floats, and Strings, you can use the Processing classes IntList, FloatList, and StringList.

An ArrayList is a resizable-array implementation of the Java List interface. It has many methods used to control and search its contents. For example, the length of the ArrayList is returned by its size()method, which is an integer value for the total number of elements in the list. An element is added to an ArrayList with the add() method and is deleted with the remove() method. The get() method returns the element at the specified position in the list. (See the above example for context.)

For a list of the numerous ArrayList features, please read the Java reference description.

arraylist 存储可变数量的对象。这类似于制作一个对象数组, 但是使用 arraylist, 可以轻松地从 arraylist 中添加和移除项, 并动态调整其大小。这可能非常方便, 但它比在使用多个元素时制作一个对象数组慢。请注意, 对于整数、浮点和字符串的可调整大小的列表, 可以使用 IntListFloatList StringList 的处理类。

 

arraylist java 列表接口的可调整大小的数组实现。它有许多用于控制和搜索其内容的方法。例如, arraylist 的长度由其大小 () 方法返回, 它是列表中元素总数的整数值。使用 add () 方法将元素添加到 arraylist , 并使用 remove () 方法将其删除。获取 () 方法返回列表中指定位置处的元素。(请参见上面的上下文示例。

 

有关众多 arraylist 功能的列表, 请阅读 java 参考说明。

Constructor

ArrayList<Type>()

ArrayList<Type>(initialCapacity)

Parameters

参数

Type

Class Name: the data type for the objects to be placed in the ArrayList.

类名称: 要放置在 arraylist 中的对象的数据类型。

initialCapacity

int: defines the initial capacity of the list; it’s empty by default

int: 定义列表的初始容量;默认情况下为空

Related

相关

IntList
FloatList
StringList



最后编辑:
作者:卡萨布兰卡
卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。