Processing, 类ArrayList
用法介绍。
构造函数
ArrayList<Type>()
ArrayList<Type>(initialCapacity)
参数
Type
类名:要放置在 ArrayList 中的对象的数据类型。initialCapacity
int:定义列表的初始容量;默认为空
说明
ArrayList
存储可变数量的对象。这类似于创建一个对象数组,但使用 ArrayList
,可以轻松地从 ArrayList 添加和删除项目,并且可以动态调整其大小。这可能非常方便,但它比使用许多元素时创建对象数组要慢。请注意,对于可调整大小的整数、浮点数和字符串列表,您可以使用处理类 IntList、FloatList 和 StringList。
ArrayList 是 Java List 接口的 resizable-array 实现。它有许多用于控制和搜索其内容的方法。例如,ArrayList
的长度由其size()
方法返回,它是列表中元素总数的整数值。使用add()
方法将元素添加到ArrayList
并使用remove()
方法删除元素。 get()
方法返回列表中指定位置的元素。 (有关上下文,请参见上面的示例。)
有关众多ArrayList
函数的列表,请阅读Java reference description。
例子
// 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);
}
}
相关用法
- Processing Array用法及代码示例
- Processing AudioSample.jump()用法及代码示例
- Processing AudioIn用法及代码示例
- Processing AudioIn.add()用法及代码示例
- Processing AudioSample.duration()用法及代码示例
- Processing AudioSample用法及代码示例
- Processing AudioSample.frames()用法及代码示例
- Processing AudioSample.pan()用法及代码示例
- Processing AudioSample.write()用法及代码示例
- Processing AudioSample.rate()用法及代码示例
- Processing AudioIn.set()用法及代码示例
- Processing AudioIn.stop()用法及代码示例
- Processing AudioSample.play()用法及代码示例
- Processing AudioSample.cue()用法及代码示例
- Processing AudioSample.resize()用法及代码示例
- Processing Amplitude用法及代码示例
- Processing AudioIn.amp()用法及代码示例
- Processing AudioSample.loop()用法及代码示例
- Processing AudioIn.start()用法及代码示例
- Processing AudioSample.channels()用法及代码示例
- Processing Amplitude.input()用法及代码示例
- Processing AudioSample.sampleRate()用法及代码示例
- Processing Amplitude.analyze()用法及代码示例
- Processing AudioIn.pan()用法及代码示例
- Processing Amplitude.stop()用法及代码示例
注:本文由纯净天空筛选整理自processing.org大神的英文原创作品 ArrayList。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。