我试图在Java中反转一个int数组。

此方法不反转数组。

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;
}

有什么问题吗?


当前回答

一种无需额外库、导入或静态引用即可进行反向操作的简便方法。

int[] a = {1,2,3,4,5,6,7,23,9}, b; //compound declaration
var j = a.length;
b = new int[j];
for (var i : a)
    b[--j] = i; //--j so you don't have to subtract 1 from j. Otherwise you would get ArrayIndexOutOfBoundsException;
System.out.println(Arrays.toString(b));

当然,如果你需要a是反向数组就用

a = b; //after the loop

其他回答

只是为了它。人们通常只需要反向排列的数组或列表上的“视图”,而不是在处理流和集合时完全不需要反向数组,而是原始数组/集合上的“反向”视图。,最好创建一个在列表/数组上具有反向视图的工具包。

所以创建你的Iterator实现,它接受一个数组或列表并提供输入。

/// Reverse Iterator
public class ReverseIterator<T> implements Iterator<T> {
  private int index;
  private final List<T> list;
  public ReverseIterator(List<T> list) {
     this.list = list;
     this.index = list.size() - 1;
  }
  public boolean hasNext() {
    return index >= 0 ? true : false;
  }
  public T next() {
    if(index >= 0) 
      return list.get(index--);
    else 
      throw new NoSuchElementException();
  }
}

数组情况的实现非常类似。当然,迭代器也可以是流或集合的源。

因此,当你想做的只是迭代数组/列表或将其提供给流或新的集合/数组时,创建一个新数组并不总是最好的。

反转一个int数组,你交换元素直到你到达中点,像这样:

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;
}

你这样做的方式是,你交换每个元素两次,所以结果与初始列表相同。

我认为如果你声明显式变量来跟踪你在每次循环迭代中交换的下标,那么遵循算法的逻辑会更容易一些。

public static void reverse(int[] data) {
    for (int left = 0, right = data.length - 1; left < right; left++, right--) {
        // swap the values at the left and right indices
        int temp = data[left];
        data[left]  = data[right];
        data[right] = temp;
    }
}

我还认为在while循环中执行这个操作更具可读性。

public static void reverse(int[] data) {
    int left = 0;
    int right = data.length - 1;

    while( left < right ) {
        // swap the values at the left and right indices
        int temp = data[left];
        data[left] = data[right];
        data[right] = temp;

        // move the left and right index pointers in toward the center
        left++;
        right--;
    }
}

以下是精简版:

我的解决方案创建了一个反转的新数组 每次迭代i时,for循环都会插入最后一个索引[array]。长度- 1]到当前索引[i] 然后通过减去当前迭代数组[(array. array.)]继续相同的过程。长度- 1)- i]从最后一个索引和插入元素到反向数组的下一个索引!

private static void reverse(int[] array) {
    int[] reversed = new int[array.length];

    for (int i = 0; i < array.length; i++) {
        reversed[i] = array[(array.length - 1) - i];
    }
    System.out.println(Arrays.toString(reversed));
}
 public static int[] reverse(int[] array) {

    int j = array.length-1;
    // swap the values at the left and right indices //////
        for(int i=0; i<=j; i++)
        {
             int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
           j--;
        }

         return array;
    }

      public static void main(String []args){
        int[] data = {1,2,3,4,5,6,7,8,9};
        reverse(data);

    }