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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。