Skip to content

Commit 77f4580

Browse files
committed
simplifying ArrayList doc page while working on #144
1 parent bf5c29a commit 77f4580

1 file changed

Lines changed: 40 additions & 28 deletions

File tree

content/api_en/include/ArrayList.xml

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,60 @@
99

1010
<usage>Web &amp; Application</usage>
1111

12+
<example>
13+
<image></image>
14+
<code>
15+
1216
<example>
1317
<image></image>
1418
<code><![CDATA[
15-
// This is a code fragment that shows how to use an ArrayList.
16-
// It won't compile because it's missing the Ball class.
19+
// These are code fragments that show how to use an ArrayList.
20+
// They won't compile because they assume the existence of a Particle class
1721
18-
// Declaring the ArrayList, note the use of the syntax "&lt;Ball&gt;" to indicate
22+
// Declaring the ArrayList, note the use of the syntax "&lt;Particle&gt;" to indicate
1923
// our intention to fill this ArrayList with Ball objects
20-
ArrayList&lt;Ball&gt; balls;
24+
ArrayList&lt;Particle&gt; particles = new ArrayList&lt;Particle&gt;;
25+
26+
// Objects can be added to an ArrayList with add()
27+
particles.add(new Particle());
28+
29+
// Particles can be pulled out of an ArrayList with get()
30+
Particle part = particles.get(0);
31+
part.display();
2132
22-
void setup() {
23-
size(200, 200);
24-
balls = new ArrayList&lt;Ball&gt;(); // Create an empty ArrayList
25-
balls.add(new Ball(width/2, 0, 48)); // Start by adding one element
33+
// The size method returns the current number of particles in the list
34+
int total = particles.size();
35+
println("The total number of particles is: " + total);
36+
37+
// You can iterate over an ArrayList two ways
38+
// The first is by counting through the elements
39+
for (int i = 0; i < particles.size(); i++) {
40+
Particle part = particles.get(i);
41+
part.display();
2642
}
2743
28-
void draw() {
29-
background(255);
30-
31-
// With an array, we say balls.length. With an ArrayList,
32-
// we say balls.size(). The length of an ArrayList is dynamic.
33-
// Notice how we are looping through the ArrayList backwards.
34-
// This is because we are deleting elements from the list.
35-
for (int i = balls.size()-1; i >= 0; i--) {
36-
Ball ball = balls.get(i);
37-
ball.move();
38-
ball.display();
39-
if (ball.finished()) {
40-
// Items can be deleted with remove().
41-
balls.remove(i);
42-
}
43-
}
44+
// The second is using an enhanced loop
45+
for (Particle part : particles) {
46+
part.display();
4447
}
4548
46-
void mousePressed() {
47-
// A new ball object is added to the ArrayList, by default to the end.
48-
balls.add(new Ball(mouseX, mouseY, ballWidth));
49+
// You can delete particles from an ArrayList
50+
particles.remove(0);
51+
println(particles.size()); // Now one less!
52+
53+
// If you are modifying an ArrayList during the loop
54+
// you cannot use the enhanced loop syntax.
55+
// In addition, when deleting in order to hit all elements,
56+
// you should loop through it backwards.
57+
for (int i = particles.size - 1; i >= 0; i--) {
58+
Particle part = particles.get(i);
59+
if (part.finished()) {
60+
particles.remove(i);
61+
}
4962
}
5063
]]></code>
5164
</example>
5265

53-
5466
<description><![CDATA[
5567
An <b>ArrayList</b> stores a variable number of objects. This is similar to making an array of objects, but with an <b>ArrayList</b>, 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.<br />
5668
<br />

0 commit comments

Comments
 (0)