reverse the 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: js array reverse

[34, 234, 567, 4].reverse();

Example 3: reverse array javascript

var rev = arr.reverse();

Example 4: reverse array javascript

const list = [1, 2, 3, 4, 5];
list.reverse();

Example 5: reverse string array java

//java program to reverse array using for loop
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: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}

Example 6: c++ reverse array

int arr[5] = {1, 2, 3, 4, 5}; //Initialize array

for(int i = 0; i < size(arr); i++) {
	//Create temporary variable to hold current value in array
	int temp = arr[i];
	//Set the current value in the array to the mirrored value in array
	arr[i] = arr[size(arr) - 1 - i];
	//Set mirrored value in array to temp, swapping the two numbers
	arr[size(arr) - 1 - i] = temp;
}

Tags:

Php Example