一种切换2个数组内容的方法

时间:2014-01-14 19:20:37

标签: java arrays

我正在尝试编写一个方法来快速将一个int []的内容分配给另一个。我想我可能在解释问题时遇到困难,因为我认为方法不能对作为参数提供的变量名执行赋值。如果问题没有说写一个方法,我可能只是声明temp int []并在main中执行赋值。我在switchThem方法中添加了print循环,以显示结果与在运行方法后打印main中的内容有何不同。

以下是问题的文字:

“编写一个名为switchThem的方法,它接受两个整数数组作为参数并切换数组的内容。考虑到数组可能有不同的大小。”

public class Ch8Scratch {
    public static void main (String[] args) {
        int[] first = { 1, 2, 3, 4, 5 };
        int[] second = { 6, 7, 8 };
        switchThem(first,second);
        System.out.println();
        for (int number : first) {
            System.out.print(number + "\t");
        }
        System.out.println();
        for (int number : second) {
            System.out.print(number + "\t");
        }
        System.out.println();
    }

    public static void switchThem (int[] array1, int[] array2) {
        int[] temp = array1;
        array1 = array2;
        array2 = temp;
        for (int number : array1) {
            System.out.print(number + "\t");
        }
        System.out.println();
        for (int number : array2) {
            System.out.print(number + "\t");
        }
    }
}

输出:

6   7   8   
1   2   3   4   5   
1   2   3   4   5   
6   7   8   

3 个答案:

答案 0 :(得分:1)

你的切换方法有问题。 它将切换array1array2(两个参数)。 但它不会从主方法切换firstsecond

告诉你的老师你不能真正用Java切换它们,因为Java没有指针(通过引用传递)。您的切换方法无法真正使first指向secondsecond指向first(您只能模拟此行为 - 请参阅Elliott Frisch的回答或我答案中的选项二。)

1)一种选择是制作firstsecond类变量。 但如果你把它们变成类变量,这不是想要的, 因为你的方法不可重复使用。 这就是我所说的。

public class Ch8Scratch {
    private static int[] first = new int[] { 1, 2, 3, 4, 5 };
    private static int[] second = new int[] { 6, 7, 8 };

    public static void main(String[] args) {
        System.out.println();
        for (int number : first) {
            System.out.print(number + "\t");
        }
        System.out.println();
        for (int number : second) {
            System.out.print(number + "\t");
        }
        System.out.println();

        switchThem();

        System.out.println();
        for (int number : first) {
            System.out.print(number + "\t");
        }
        System.out.println();
        for (int number : second) {
            System.out.print(number + "\t");
        }
        System.out.println();
    }

    public static void switchThem() {
        int[] temp = first;
        first = second;
        second = temp;
    }

}

2)可能最简单的选择是将它们表示为数组数组。然后遵循这种模式。这样您就可以使用first[0]切换second[0]。从技术上讲,它仍然没有切换两个1D阵列。但它与Java中最接近。

public class Ch8Scratch {

    public static void main(String[] args) {

        int[][] first = new int[][] { new int[] {1, 2, 3, 4, 5 }};
        int[][] second = new int[][] { new int[] {6, 7, 8 }};


        System.out.println();
        for (int number : first[0]) {
            System.out.print(number + "\t");
        }
        System.out.println();
        for (int number : second[0]) {
            System.out.print(number + "\t");
        }
        System.out.println();

        switchThem(first, second);

        System.out.println();
        for (int number : first[0]) {
            System.out.print(number + "\t");
        }
        System.out.println();
        for (int number : second[0]) {
            System.out.print(number + "\t");
        }
        System.out.println();
    }

    public static void switchThem(int[][] arr1, int[][] arr2) {
        int[] temp = arr1[0];
        arr1[0] = arr2[0];
        arr2[0] = temp;
    }

}

答案 1 :(得分:0)

你不能修改来电者的参考资料,但是这样的东西可能就是你想要的 -

public static void switchThem (java.util.List<Integer> al, 
                               java.util.List<Integer> bl) 
{
    java.util.List<Integer> t = new java.util.ArrayList<Integer>(al);
    al.clear();
    al.addAll(bl);
    bl.clear();
    bl.addAll(t);
}

public static void main(String[] args) {
    Integer[] first = { 1, 2, 3, 4, 5 };
    Integer[] second = { 6, 7, 8 };
    System.out.println("First: " + java.util.Arrays.toString(first));
    System.out.println("Second: " + java.util.Arrays.toString(second));
    java.util.List<Integer> fList = new java.util.ArrayList<Integer>();
    fList.addAll(java.util.Arrays.asList(first));
    java.util.List<Integer> sList = new java.util.ArrayList<Integer>();
    sList.addAll(java.util.Arrays.asList(second));

    switchThem(fList, sList);
    first = fList.toArray(new Integer[fList.size()]);
    second = sList.toArray(new Integer[sList.size()]);
    System.out.println("First: " + java.util.Arrays.toString(first));
    System.out.println("Second: " + java.util.Arrays.toString(second));
}

输出

First: [1, 2, 3, 4, 5]
Second: [6, 7, 8]
First: [6, 7, 8]
Second: [1, 2, 3, 4, 5]

答案 2 :(得分:0)

好的,你不能实际切换数组(因为它在Java中是不可能的),但你可以切换他们的内容。假设如果数组没有相同的长度,你想要最大化可能的元素数量:

public void switch(int[]a, int[] b) {
  int max = Math.min(a.length, b.length);
  for(int i = 0; i< max; i++) {
    int temp = a[i];
    a[i] = b[i];
    b[i] = temp;
  }
}

这样切换:

First:  [1, 2, 3, 4, 5]
Second: [6, 7, 8]
First:  [6, 7, 8, 4, 5]
Second: [1, 2, 3]