-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathArray.xml
More file actions
executable file
·128 lines (96 loc) · 3.56 KB
/
Array.xml
File metadata and controls
executable file
·128 lines (96 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<name>Array</name>
<category>Data</category>
<subcategory>Composite</subcategory>
<usage>Web & Application</usage>
<example>
<image></image>
<code><![CDATA[
int[] numbers = new int[3];
numbers[0] = 90; // Assign value to first element in the array
numbers[1] = 150; // Assign value to second element in the array
numbers[2] = 30; // Assign value to third element in the array
int a = numbers[0] + numbers[1]; // Sets variable 'a' to 240
int b = numbers[1] + numbers[2]; // Sets variable 'b' to 180
]]></code>
</example>
<example>
<image></image>
<code><![CDATA[
int[] numbers = { 90, 150, 30 }; // Alternate syntax
int a = numbers[0] + numbers[1]; // Sets variable 'a' to 240
int b = numbers[1] + numbers[2]; // Sets variable 'b' to 180
]]></code>
</example>
<example>
<image></image>
<code><![CDATA[
int degrees = 360;
float[] cos_vals = new float[degrees];
// Use a for() loop to quickly iterate
// through all values in an array.
for (int i=0; i < degrees; i++) {
cos_vals[i] = cos(TWO_PI/degrees * i);
}
]]></code>
</example>
<example>
<image></image>
<code><![CDATA[
float[] randoms = new float[100];
for (int i = 0; i < randoms.length; i++) {
randoms[i] = random(100);
}
// You can also use an enhanced loop
// to access the elements of an array
for (float val : randoms) {
println(val);
}
// This works with arrays of objects, too,
// but not when first making the array
PVector[] vectors = new PVector[5];
for (int i = 0; i < vectors.length; i++) {
vectors[i] = new PVector();
}
// The syntax only applies when iterating
// over an existing array
for (PVector v : vectors) {
point(v.x, v.y);
}
]]></code>
</example>
<description><![CDATA[
An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first element in the array is <b>[0]</b>, the second element is <b>[1]</b>, and so on. Arrays are similar to objects, so they must be created with the keyword <b>new</b>.<br/>
<br/>
Each array has a variable <b>length</b>, which is an integer value for the total number of elements in the array. Note that since index numbering begins at zero (not 1), the last value in an array with a <b>length</b> of 5 should be referenced as <b>array[4]</b> (that is, the <b>length</b> minus 1), not <b>array[5]</b>, which would trigger an error.<br/>
<br/>
Another common source of confusion is the difference between using <b>length</b> to get the size of an array and <b>length()</b> to get the size of a String. Notice the presence of parentheses when working with Strings. (<b>array.length</b> is a variable, while <b>String.length()</b> is a method specific to the String class.)
]]></description>
<syntax>
<c>datatype</c>[] <c>var</c>
<c>var</c>[<c>element</c>] = <c>value</c>
<c>var</c>.length
</syntax>
<parameter>
<label>datatype</label>
<description><![CDATA[any primitive or compound datatype, including user-defined classes]]></description>
</parameter>
<parameter>
<label>var</label>
<description><![CDATA[any valid variable name]]></description>
</parameter>
<parameter>
<label>element</label>
<description><![CDATA[int: must not exceed the length of the array minus 1]]></description>
</parameter>
<parameter>
<label>value</label>
<description><![CDATA[data to assign to the array element; must be the same datatype as the array]]></description>
</parameter>
<returns></returns>
<related></related>
<availability>1.0</availability>
<type>Object</type>
<partof>PDE</partof>
</root>