To reverse an int array, you swap items up until you reach the midpoint, like this:
for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
The way you are doing it, you swap each element twice, so the result is the same as the initial list.
Answer from 3lectrologos on Stack OverflowTo reverse an int array, you swap items up until you reach the midpoint, like this:
for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
The way you are doing it, you swap each element twice, so the result is the same as the initial list.
With Commons.Lang, you could simply use
ArrayUtils.reverse(int[] array)
Most of the time, it's quicker and more bug-safe to stick with easily available libraries already unit-tested and user-tested when they take care of your problem.
Videos
You are assigning values from an uninitialized array d to x - that's where the zeroes (default value for an int in Java) are coming from.
IIUC, you're mixing two reversing strategies.
If you're creating a new array, you needn't run over half of the original array, but over all of it:
public static int[] reverse(int[] x) {
int[] d = new int[x.length];
for (int i = 0; i < x.length; i++) {
d[i] = x[x.length - 1 -i];
}
return d;
}
Alternatively, if you want to reverse the array in place, you don't need a temp array, only a single variable (at most - there are also ways to switch two ints without an additional variable, but that's a different question):
public static int[] reverseInPlace(int[] x) {
int tmp;
for (int i = 0; i < x.length / 2; i++) {
tmp = x[i];
x[i] = x[x.length - 1 - i];
x[x.length - 1 - i] = tmp;
}
return x; // for completeness, not really necessary.
}
Here is a short way to do it.
public static int[] reverse(int[] x)
{
int[] d = new int[x.length]; //create new array
for (int i=x.length-1; i >= 0; i--) // revered loop
{
d[(x.length-i-1)]=x[i]; //setting values
}
return d; // returns the new reversed array
}
Collections.reverse() can do that job for you if you put your numbers in a List of Integers.
List<Integer> list = Arrays.asList(1, 4, 9, 16, 9, 7, 4, 9, 11);
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
Output:
[1, 4, 9, 16, 9, 7, 4, 9, 11]
[11, 9, 4, 7, 9, 16, 9, 4, 1]
If you want to reverse the array in-place:
Collections.reverse(Arrays.asList(array));
It works since Arrays.asList returns a write-through proxy to the original array.
If you want to reverse the elements in the array, you need to write method swap() like this:
static void swap(int[] arr) {
for (int start = 0; start < arr.length / 2; start++) {
int tmp = arr[arr.length - 1 - start];
arr[arr.length - 1 - start] = arr[start];
arr[start] = tmp;
}
}
You can use the ‘reverse’ method present in the collections framework. But for this, you first need to convert an array to a list as the ‘reverse’ method takes the list as an argument.
static void swap(Integer arr[]) {
Collections.reverse(Arrays.asList(arr));
System.out.println(Arrays.toString(myArray));
}
Arrays in Java are indexed from 0 to length - 1, not 1 to length, therefore you should be assign your variable accordingly and use the correct comparison operator.
Your loop should look like this:
for (int counter = myArray.length - 1; counter >= 0; counter--) {
use myArray.length-1
for(int counter=myArray.length-1; counter >= 0;counter--){
System.out.println(myArray[counter]);
}