如何在java中比较和交换两个数组

如何在java中比较和交换两个数组,java,arrays,swap,Java,Arrays,Swap,我尝试了以下代码,但仍有错误。在比较两个数组并检查它们是否指向一个数组时,我似乎遇到了一个问题。在交换两个数组以便它们可以交换它们的值时,我也遇到了一个问题。此外,删除数组中相同的值似乎非常困难。下面的代码是我对它们的描述,但即使尝试了不同的方法解决问题,我仍然会出错。请帮助我了解如何比较两个数组,特别是在这些代码上 public class ArraySwap { public static void main(String[] args) { //Some test

我尝试了以下代码,但仍有错误。在比较两个数组并检查它们是否指向一个数组时,我似乎遇到了一个问题。在交换两个数组以便它们可以交换它们的值时,我也遇到了一个问题。此外,删除数组中相同的值似乎非常困难。下面的代码是我对它们的描述,但即使尝试了不同的方法解决问题,我仍然会出错。请帮助我了解如何比较两个数组,特别是在这些代码上

public class ArraySwap {

    public static void main(String[] args) {
        //Some test code
        ArraySwap swapper = new ArraySwap();
        int[] a = {1, 2, 3}; //initialize array a[]
        int[] b = a;  //initialize array b[]
        System.out.println(swapper.arrayEquals(a, b));

        int[] arr = {1, 1, 2, 3, 4, 1, 5, 6, 8, 7, 9, 9, 8, 9, 1};
        ArraySwap c = new ArraySwap();
        arr = c.removeDuplicates(arr);
        for (Integer i : arr) {
            System.out.print(i);
        }
        System.out.println();

    }

    /*
      Return true if the values in a are the same as the values in b
     */
    public boolean arrayValuesEqual(int[] a, int[] b) {
        if (Arrays.equals(a, b)) {
            return true;   //returning true
        } else {
            return false;
        }
    }

    /*
      Return true if a and b point to the same array
     */
    public boolean arrayEquals(int[] a, int[] b) {
        if (a.length == b.length) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * Swap a and b WITHOUT doing an elementwise copy.
     */
    public void swap(int[] a, int[] b) {

        int temp = a[0];
        a[0] = b[0];
        b[0] = temp;
    }
}
/*
     * Returns true if a and b have the same name
     */

    public boolean sameName(Person a, Person b) {
        if (a == b) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * Given an array of positive integers, removes duplicates
     * in the array.
     * @return a contiguous array with the remaining integers 
     * with a length equal to the number of remaining integers
     */
    public int[] removeDuplicates(int[] integers) {
        int[] toReturn = new int[];
        for (int i = 0; i > integers.length; i++) {
            if (i + 1 > integers.length) {
                if (integers[i] == integers[i + 1]) {
                    remove.integers[i];
                }

            }
            toReturn = integers[i];
        }
        return toReturn;
    }
}

你似乎一次问了几个问题,但并不清楚你的目标是什么

在您的arrayValuesEqual()中,您将数组a和b与.equals进行比较。此方法将比较数组的浅拷贝。如果要进行深度复制,应使用.deepEquals()。请参见此处的更多信息:

arrayEquals方法中,如果它们指向同一个数组,说明将读取,但您所做的只是检查长度。。这并不能证明包含的内容是相同的

您的swap方法-如果是多维数组,该方法有效吗?如果它们不是相同长度的数组呢?您似乎在为特定的用例编写代码,而没有考虑各种测试用例


您的sameName方法-这不是比较对象的方式。==比较对象引用,检查两个操作数是否指向同一个对象(不是等效对象,即相同的对象)。您将需要覆盖Person对象的equals方法。

但我仍然收到错误。。。也许告诉我们错误是什么,我们就不需要使用我们神秘的心灵感应技能……为什么你在你的问题中抛弃了这么多随机的和不相关的方法?而且,
sameName
是错误的。不要比较引用等式。