Reverse the order of the given short type value array in Java
Description
The following code shows how to reverse the order of the given short type value array.
Example
//from w w w. j ava 2 s .co m
import java.util.Arrays;
public class Main {
public static void main(String[] argv){
short[] data = new short[]{1,2,3,4,5};
System.out.println(Arrays.toString(data));
reverse(data);
System.out.println(Arrays.toString(data));
}
public static void reverse(short[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
short tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »