Java Reference
In-Depth Information
ids.sort(compareDescending);
print("Sorted in descending order, ids = " + ids);
ids = 30,10,40,20
Sorted ids = 10,20,30,40
Sorted in descending order, ids = 40,30,20,10
Adding and Removing Elements at Ends
The following four methods let you add and remove elements at the beginning and end of
an array:
unshift(value1, value2,...) : The unshift() method
prepends the arguments to the array, so they appear in the
beginning of the array in the same order they appear as
arguments. It returns the new length of the array. Indexes of the
existing elements are adjusted to make room for new elements.
shift() : The shift() method removes and returns the first
element of the array, shifting all other elements one position to
the left.
push(value1, value2,...) : The push() method works the same
as the unshift() method, except that it adds elements at the end
of the array.
pop() : The pop() method works the same as the shift() method,
except it removes and returns the last element of the array
Here are examples of using these methods:
var ids = [10, 20, 30];
print("ids: " + ids);
ids.unshift(100, 200, 300);
print("After ids.unshift(100, 200, 300): " + ids);
ids.shift();
print("After ids.shift(): " + ids);
ids.push(1, 2, 3);
print("After ids.push(1, 2, 3): " + ids);
ids.pop();
print("After ids.pop(): " + ids);
 
Search WWH ::




Custom Search