Java中数组的逆序?

huangapple go评论100阅读模式
英文:

Reverse the order of array in java?

问题

我正在尝试编写一个反转数组的函数,但它的工作方式不如我所希望的。

public class ReverseArray{
    public static int[] reverseArray(int[] array){
        int[] revArray=array;
        int i=0;
        int j=revArray.length-1;
        while(i<=j){
            swap(i,j,revArray);
            j--;
            i++;
        }
        return revArray;
    }
    
    public static void swap(int i, int j, int[] revArray){
        int temp;
        temp=revArray[i];
        revArray[i]=revArray[j]; 
        revArray[j]=temp;
    }

    public static void main(String []args){
         
        int[] array={2,4,6,8,10,12,14,16};
         
         
        int[] revArray=reverseArray(array);
         
        for(int i=0;i<array.length;i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
         
         
        for(int i=0;i<revArray.length;i++){
            System.out.print(revArray[i]+" ");
        }
    }
} 

输出16 14 12 10 8 6 4 2   
       16 14 12 10 8 6 4 2 

但是,如果我像这样做:

int[] array={2,4,6,8,10,12,14,16};
         
         for(int i=0;i<array.length;i++){
             System.out.print(array[i]+" ");
         }
         
         int[] revArray=reverseArray(array);
         
         
         System.out.println();
         
         
         for(int i=0;i<revArray.length;i++){
             System.out.print(revArray[i]+" ");
         }
         
输出2 4 6 8 10 12 14 16   
       16 14 12 10 8 6 4 2 

我不明白为什么在我在打印数组之前调用函数 reverseArray,两个数组都被反转了。

英文:

I am trying to write a function to reverse array, but it doesn't work like I want to.

public class ReverseArray{
    public static int[] reverseArray(int[] array){
        int[] revArray=array;
        int i=0;
        int j=revArray.length-1;
        while(i&lt;=j){
            swap(i,j,revArray);
            j--;
            i++;
        }
        return revArray;
    }
    
    public static void swap(int i, int j, int[] revArray){
        int temp;
        temp=revArray[i];
        revArray[i]=revArray[j]; 
        revArray[j]=temp;
    }

    public static void main(String []args){
         
        int[] array={2,4,6,8,10,12,14,16};
         
         
        int[] revArray=reverseArray(array);
         
        for(int i=0;i&lt;array.length;i++){
            System.out.print(array[i]+&quot; &quot;);
        }
        System.out.println();
         
         
        for(int i=0;i&lt;revArray.length;i++){
            System.out.print(revArray[i]+&quot; &quot;);
        }
    }
} 

Output:16 14 12 10 8 6 4 2   
       16 14 12 10 8 6 4 2 

But when I do it like this:

int[] array={2,4,6,8,10,12,14,16};
         
         for(int i=0;i&lt;array.length;i++){
             System.out.print(array[i]+&quot; &quot;);
         }
         
         int[] revArray=reverseArray(array);
         
         
         System.out.println();
         
         
         for(int i=0;i&lt;revArray.length;i++){
             System.out.print(revArray[i]+&quot; &quot;);
         }
Output:2 4 6 8 10 12 14 16   
       16 14 12 10 8 6 4 2 

I don't understand why both arrays are reversed if I call the function revArray before I print them.

答案1

得分: 0

你的reverseArray()方法将array赋值给变量revArray,因此这两个变量指向内存中的同一个数组。

为了避免这种情况,可以尝试像这样做:

public static int[] reverseArray(int[] array){
    int[] revArray = Arrays.copyOf(array, array.length);
    int i = 0;
    int j = revArray.length - 1;
    while(i <= j){
        swap(i, j, revArray);
        j--;
        i++;
    }
    return revArray;
}
英文:

Your reverseArray() method assigns array to the variable revArray, thus the 2 variables pointing the same array in memory.

To avoid that, try something like this:

public static int[] reverseArray(int[] array){
    int[] revArray= Arrays.copyOf(array, array.length);
    int i=0;
    int j=revArray.length-1;
    while(i&lt;=j){
        swap(i,j,revArray);
        j--;
        i++;
    }
    return revArray;
}

答案2

得分: 0

正如评论中所提到的,问题在于你在方法中引用了同一个数组。因此,当你在方法中对它进行更改时,它会改变原始数组。但是,要反转一个数组,并不需要这么复杂的操作。可以参考以下代码:

int[] array={2,4,6,8,10,12,14,16};
int length = array.length;
int[] array1 = new int[length];
for(int i = 0 ; i < length; i++) {
  array1[length - 1 - i] = array[i];
}
System.out.println(Arrays.toString(array1)); // 输出 [16, 14, 12, 10, 8, 6, 4, 2]
英文:

As it has been mentioned in the comments, the problem is you are refering to the same array in. So, when you change it in the methods, it changes the original. But, to reverse an array you don't need this much of a complexity.
check this:

int[] array={2,4,6,8,10,12,14,16};
int length = array.length;
int[] array1 = new int[length];
for(int i = 0 ; i &lt; length; i++) {
  array1[length - 1 - i] = array[i];
}
System.out.println(Arrays.toString(array1)); // prints [16, 14, 12, 10, 8, 6, 4, 2]

huangapple
  • 本文由 发表于 2020年10月20日 23:10:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定