数组中的Java-swap

数组中的Java-swap,java,arrays,swap,Java,Arrays,Swap,我想交换数组中的2个值。。。 这是我的代码: static void swap (Integer[] input, int i, int j) { /* * Assure indices are allright! */ if (i < 0 || i >= input.length) throw new IllegalArgumentException("Incorrect i!"); if (j < 0 |

我想交换数组中的2个值。。。 这是我的代码:

    static void swap (Integer[] input, int i, int j) {

    /*
     * Assure indices are allright!
     */
    if (i < 0 || i >= input.length)
        throw new IllegalArgumentException("Incorrect i!");
    if (j < 0 || j >= input.length)
        throw new IllegalArgumentException("Incorrect j!");

    /*
     * (***) This method does not work... Why? (***)
     */
     input[i] ^= input[j];
     input[j] ^= input[i];
     input[i] ^= input[j];

    /*
     * This works...
     */
    /*int temp = input[j];
    input[j] = input[i];
    input[i] = temp;*/
}
为什么中间的方法不起作用???< /p> < p>这是一个版本,它不是一个非常有效的优化,它使用java这样的动态语言。相反,我建议你尝试;你已经有了一个使用临时变量的工作交换,这对我来说似乎是最好的解决方案,所以我想我会给你展示一个正确的xor交换。。。最后一点注意,如果i==j,请确保不要尝试此操作


当i等于j时,如果你尝试减法或异或,你将得到零。

它确实有效,至少有时是这样。您应该通过示例输入和输出更好地解释问题。这是一个有效的测试:

import org.junit.Assert;
import org.junit.Test;

public final class Swapper
{

  @Test
  public void testSwap()
  {
    Integer[] input = {1, 2, 3};
    swap(input, 0, 2);
    Assert.assertArrayEquals(new Integer[]{3, 2, 1}, input);
  }

  @Test
  public void testNoop()
  {
    Integer[] input = {1, 2, 3};
    swap(input, 1, 1);
    Assert.assertArrayEquals(new Integer[]{1, 2, 3}, input);
  }

  static void swap(Integer[] input, int i, int j)
  {
    if (i < 0 || i >= input.length)
      throw new IndexOutOfBoundsException(Integer.toString(i));
    if (j < 0 || j >= input.length)
      throw new IndexOutOfBoundsException(Integer.toString(j));
    input[i] = input[i] + input[j];
    input[j] = input[i] - input[j];
    input[i] = input[i] - input[j];
  }

}
正如您已经发现的,当i和j相等时,您的方法将失败。通过在验证索引范围后检查该条件,可以很容易地解决这个问题


这就是说,这是一种糟糕的交换元素的方式,因为Java处理int等基本类型和Integer等对象对应类型的方式。您正在创建大量整数对象实例,这可能比使用局部变量来保存传输中的元素慢得多。

它怎么不起作用?你期待什么?你得到了什么?你正在设置input[j]=input[i]-input[j];,这将如何为您提供正确的输入结果[j]。如果你的意思是我们+=它会更接近,尽管我认为你的输入[I]仍然不正确。实际上,我刚刚测试了这个。它对我有效,除了i==j的情况。当您尝试它时,会出现什么问题?对于特殊情况i==j,它将失败。你比自己聪明:如果你在问题中真的提到这一点会更好;似乎有不少人花时间试图理解你在做什么,甚至自己测试。然而,很明显,为什么在i==j的情况下,一切都变为零。这个方法根本没有什么不好的地方——它只是与你所知道的不同。当然,这不是一个交换变量的好方法,但是说它不起作用是完全不正确的,因为它是一个坏版本的。。。。该方法本身工作正常,除非i==j。@david编辑解释了它的破坏方式,而不是仅仅说明它。是的,我注意到了。我取消了我的否决票。你确定testNoop通过了吗?@DavidWallace它没有通过。
import org.junit.Assert;
import org.junit.Test;

public final class Swapper
{

  @Test
  public void testSwap()
  {
    Integer[] input = {1, 2, 3};
    swap(input, 0, 2);
    Assert.assertArrayEquals(new Integer[]{3, 2, 1}, input);
  }

  @Test
  public void testNoop()
  {
    Integer[] input = {1, 2, 3};
    swap(input, 1, 1);
    Assert.assertArrayEquals(new Integer[]{1, 2, 3}, input);
  }

  static void swap(Integer[] input, int i, int j)
  {
    if (i < 0 || i >= input.length)
      throw new IndexOutOfBoundsException(Integer.toString(i));
    if (j < 0 || j >= input.length)
      throw new IndexOutOfBoundsException(Integer.toString(j));
    input[i] = input[i] + input[j];
    input[j] = input[i] - input[j];
    input[i] = input[i] - input[j];
  }

}