Arrays
Sorting
Sorts the specified array or List.
If comparator or func is specified it would be used to compare two elements in the array (or List). When either of them is not specified, the set of Pnuts's comparison operator <, >, = is used to compare elements.
e.g.
a = [5, 2, 3]
sort(a) ==> [2, 3, 5]
a ==> [2, 3, 5]
|
v = Vector()
v.addElement(2)
v.addElement(3)
v.addElement(1)
sort(v)
v ==> [1, 2, 3]
|
func takes two parameter and returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
e.g.
a = [5, 2, 3]
sort(a, function (x, y) y - x) ==> [5, 3, 2]
a ==> [5, 3, 2]
|
Copy
- arraycopy(src [], dst [] )
- arraycopy(src [], int idx0 ,
dst [], int idx1 , int length)
|
arraycopy() calls the System.arraycopy() method to copy the specified source array to the destination array.
e.g.
a = [1, 2, 3]
b = [0, 0, 0, 4, 5]
arraycopy(a, b)
b ==> [1, 2, 3, 4, 5]
|
Sublist
- array [ start .. end ]
- array [ start ]
|
array[start..end] returns a partial list of list.
When length is omitted, it returns a partial list from start to the end.
e.g.
a = [1,2,3,4]
a[1..2] ==> [2, 3]
a[1..] ==> [2, 3, 4]
|
List Processing
map() applies function to each element of list and
creates an array from the results.
e.g.
> map([1, 2, 3], function (x) x * 100)
[100, 200, 300]
|
filter() applies function to each element of list
and build an array from the elements that the function returns true.
e.g.
> filter([1, 2, 3], function (x) x > 1)
[2, 3]
|
Conversion to Primitive Array Type
(int[])[1,2,3,4,5]
(float[])[1,2,3,4,5]
(byte[])[#1,#2,#3,#4,#5]
(char[])['a', 'b']
(boolean[])[true, false]
|
Type cast to an array type makes a new array of the specified type and
copy the elements to it. If one of the elements cannot assign to the resulting array, ClassCastException is thrown.
Array concatenation can be used for the same purpose.
int[0] + [1,2,3,4,5]
float[0] + [1,2,3,4,5]
byte[0] + [#1,#2,#3,#4,#5]
char[0] + ['a', 'b']
boolean[0] + [true, false]
|