Array element reverse

In this chapter you will learn:

  1. How to reverse elements in an array
  2. Reverses the order of the given long type value array

Reverses the order of an array

import java.util.Arrays;
//from  j a v a2 s  .c om
public class Main {
  public static void reverse(byte[] array) {
    if (array == null) {
      return;
    }
    int i = 0;
    int j = array.length - 1;
    byte tmp;
    while (j > i) {
      tmp = array[j];
      array[j] = array[i];
      array[i] = tmp;
      j--;
      i++;
    }
  }

  public static void main(String[] args) {
    byte[] b1 = new byte[] { 3, 2, 5, 4, 1 };
    for (byte b : b1) {
      System.out.println(b);
    }

    reverse(b1);
    for (byte b : b1) {
      System.out.println(b);
    }

  }

}

Output:

Reverses the order of the given long type value array

public class Main {
  public static void reverse(long[] array) {
      if (array == null) {
          return;
      }/*  j  a v  a2s .  co  m*/
      int i = 0;
      int j = array.length - 1;
      long tmp;
      while (j > i) {
          tmp = array[j];
          array[j] = array[i];
          array[i] = tmp;
          j--;
          i++;
      }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to remove duplicate element from array
  2. Remove the element at the specified position from the specified array.
Home » Java Tutorial » Array
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array Search
Array sort
Array to List
Convert array to Set
Array fill value
Array to String
Array element reverse
Array element delete
Array shuffle
Array element append
Array min / max value
Sub array search
Get Sub array
Array dimension reflection
Array clone