How do I reverse an int array in Java?

2018-12-31 02:19发布

I am trying to reverse an int array in Java.

This method does not reverse the array.

for(int i = 0; i < validData.length; i++)
{
    int temp = validData[i];
    validData[i] = validData[validData.length - i - 1];
    validData[validData.length - i - 1] = temp;
}

What is wrong with it?

30条回答
有味是清欢
2楼-- · 2018-12-31 03:18

Your program will work for only length = 0, 1. You can try :

int i = 0, j = validData.length-1 ; 
while(i < j)
{
     swap(validData, i++, j--);  // code for swap not shown, but easy enough
}
查看更多
ら面具成の殇う
3楼-- · 2018-12-31 03:18

Using the XOR solution to avoid the temp variable your code should look like

for(int i = 0; i < validData.length; i++){
    validData[i] = validData[i] ^ validData[validData.length - i - 1];
    validData[validData.length - i - 1] = validData[i] ^ validData[validData.length - i - 1];
    validData[i] = validData[i] ^ validData[validData.length - i - 1];
}

See this link for a better explanation:

http://betterexplained.com/articles/swap-two-variables-using-xor/

查看更多
何处买醉
4楼-- · 2018-12-31 03:18

2 ways to reverse an Array .

  1. Using For loop and swap the elements till the mid point with time complexity of O(n/2).

    private static void reverseArray() {
    int[] array = new int[] { 1, 2, 3, 4, 5, 6 };
    
    for (int i = 0; i < array.length / 2; i++) {
        int temp = array[i];
        int index = array.length - i - 1;
        array[i] = array[index];
        array[index] = temp;
    }
    System.out.println(Arrays.toString(array));
    

    }

  2. Using built in function (Collections.reverse())

    private static void reverseArrayUsingBuiltInFun() {
    int[] array = new int[] { 1, 2, 3, 4, 5, 6 };
    
    Collections.reverse(Ints.asList(array));
    System.out.println(Arrays.toString(array));
    

    }

    Output : [6, 5, 4, 3, 2, 1]

查看更多
高级女魔头
5楼-- · 2018-12-31 03:20

Simple for loop!

for (int start = 0, end = array.length - 1; start <= end; start++, end--) {
    int aux = array[start];
    array[start]=array[end];
    array[end]=aux;
}
查看更多
素衣白纱
6楼-- · 2018-12-31 03:20
public static void main (String args[]){

    //create  array
    String[] stuff ={"eggs","lasers","hats","pie","apples"};

    //print out  array
    for(String x :stuff)
        System.out.printf("%s ", x);
            System.out.println();

            //print out array in reverse order
            for(int i=stuff.length-1; i >= 0; i--)
                System.out.printf("%s ",stuff[i]);  

}
查看更多
大哥的爱人
7楼-- · 2018-12-31 03:21
public class TryReverse {
    public static void main(String[] args) {        
        int [] array = {2,3,4,5,6,7,8,9};       
        reverse(array);
        for(int i=0; i<array.length; ++i)
            System.out.print(array[i] + " ");
    }
    public static void reverse (int [] array){
        for(int start=0, end=array.length-1; start<=end; start++, end--){
            int aux = array[start];
            array[start]=array[end];
            array[end]=aux;
        }
    }
}
查看更多
登录 后发表回答