Array element reverse
In this chapter you will learn:
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:
- How to remove duplicate element from array
- Remove the element at the specified position from the specified array.
Home » Java Tutorial » Array