Array Reverse

From SwiftAPI

Jump to: navigation, search

Contents

C++

#include <algorithm>
using namespace std;
 
int a[] = { 2, 1, 2, 5, 0 };
reverse(a, a+5);
// a == [0, 5, 2, 1, 2]

Java

for Lists:

import java.util.Arrays;
import java.util.Collections;
 
List<Integer> a = Arrays.asList( 2, 1, 2, 5, 0 );
Collections.reverse(a);
// a == [0, 5, 2, 1, 2]

for arrays of objects (not primitive arrays):

import java.util.Arrays;
import java.util.Collections;
 
Integer[] b = { 2, 1, 2, 5, 0 };
List<Integer> a = Arrays.asList( b );
Collections.reverse(a);
// b == [0, 5, 2, 1, 2]

JavaScript

developer.mozilla.org

var a = [2, 1, 2, 5, 0];
a.reverse();
// a == [0, 5, 2, 1, 2]

Perl

perldoc

reverse LIST
@a = (2, 1, 2, 5, 0);
@a = reverse @a;
# @a == (0, 5, 2, 1, 2)

PHP

php.net

array array_reverse ( array $array [, bool $preserve_keys = false ] )
$a = array(2, 1, 2, 5, 0);
$a = array_reverse($a);
// $a == array(0, 5, 2, 1, 2)

Python

a = [2, 1, 2, 5, 0]
a.reverse()
# a == [0, 5, 2, 1, 2]

Ruby

array.reverse

Ruby-Doc.org

array.reverse -> an_array
a = [2, 1, 2, 5, 0]
a = a.reverse
# a == [0, 5, 2, 1, 2]

array.reverse!

Ruby-Doc.org

array.reverse! -> array
a = [2, 1, 2, 5, 0]
a.reverse!
# a == [0, 5, 2, 1, 2]

Swift

var a = [2, 1, 2, 5, 0]
a = a.reverse()
// a == [0, 5, 2, 1, 2]

Visual Basic .NET

msdn

System.Array : Public Shared Sub Reverse ( _
    array As Array _
)
Dim a() As Integer = New Integer() {2, 1, 2, 5, 0}
Array.Reverse(a)
Personal tools