reverse elements in an array code example
Example 1: javascript reverse array
var arr = [34, 234, 567, 4];
print(arr);
var new_arr = arr.reverse();
print(new_arr);
Example 2: how to print array backwards
var array = ['a','b','c','d','e','f','g']
var j = array.length
for(var i = 0; i < array.length ; i++){
console.log(array[j])
j=j-1 }
Example 3: reverse string array java
public class ReverseArrayDemo
{
public static void main(String[] args)
{
int[] arrNumbers = new int[]{2, 4, 6, 8, 10};
System.out.println("Given array: ");
for(int a = 0; a < arrNumbers.length; a++)
{
System.out.print(arrNumbers[a] + " ");
}
System.out.println("Reverse array: ");
for(int a = arrNumbers.length - 1; a >= 0; a--)
{
System.out.print(arrNumbers[a] + " ");
}
}
}
Example 4: c++ reverse array
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < size(arr); i++) {
int temp = arr[i];
arr[i] = arr[size(arr) - 1 - i];
arr[size(arr) - 1 - i] = temp;
}
Example 5: reverse string array java
import java.util.Arrays;
public class ReverseStringArrayInJava
{
public static void main(String[] args)
{
String[] strHierarchy = new String[]{"Junior Developer","Senior Developer","Team Lead","Project Manager","Senior Manager","CEO"};
System.out.println("Given string array: " + Arrays.toString(strHierarchy));
for(int a = 0; a < strHierarchy.length / 2; a++)
{
String strTemp = strHierarchy[a];
strHierarchy[a] = strHierarchy[strHierarchy.length - a - 1];
strHierarchy[strHierarchy.length - a - 1] = strTemp;
}
System.out.println("Reversed string array: ");
for(int a = 0; a < strHierarchy.length; a++)
{
System.out.println(strHierarchy[a]);
}
}
}
Example 6: reverse array javascript
function reverseArray (arr){
if (arr.length === 0){
return []
}
return [arr.pop()].concat(reverseArray(arr))
}
console.log(reverseArray([1, 2, 3, 4, 5]))