-1

I'm trying to write a method to quickly assign the contents of one int[] to the other. I think I might be having difficulty interpreting the problem because I don't think methods can perform assignment on a variable name provided as an argument. If the problem didn't say to write a method, I would probably just declare the temp int[] and perform the assignments in main. I added print loops to the switchThem method to show how the result differs from printing the contents in main after running the method.

Here's the text of the problem:

"Write a method called switchThem that accepts two integer arrays as parameters and switches the contents of the arrays. Take into account that the arrays may be of different sizes."

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

Output:

6   7   8   
1   2   3   4   5   
1   2   3   4   5   
6   7   8   
admdrew
  • 3,790
  • 4
  • 27
  • 39
Rikki Gibson
  • 4,136
  • 23
  • 34
  • "Take into account" may also mean that you signal the caller that the operation can't be executed with arrays of different size. – Hauke Ingmar Schmidt Jan 14 '14 at 19:24
  • "I think I might be having difficulty interpreting the problem because I don't think methods can perform assignment on a variable name provided as an argument" You probably are, as to me it sounds like you're just passing an argument to a method, and then the method operates on the argument. – MxLDevs Jan 14 '14 at 19:28
  • The reason why they're asking you to provide a method is so you can re-use your switch method without having to constantly write it again and again. – MxLDevs Jan 14 '14 at 19:32
  • OP should check this. http://stackoverflow.com/questions/40480/is-java-pass-by-reference – peter.petrov Jan 14 '14 at 19:45

3 Answers3

1

Your switch them method has a problem. It will switch array1 and array2 (the two arguments). But it will not switch first and second from the main method.

Tell your teacher that you cannot really switch them in Java, as Java has no pointers (passing by reference). There's no way your switch method can really make first point to second and second point to first (you can only simulate this behavior - see e.g. the answer from Elliott Frisch or option two in my answer).

1) One option is to make first and second class variables. But if you make them class variables this is not what is wanted, as your method will not be reusable. Here is what I am talking about.

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) Probably the simplest option is to represent them as array of arrays. And then follow this kind of pattern. This way you're switching first[0] with second[0]. So technically it is still not doing switching of two 1D arrays. But it is as closest as it could be in 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;
    }

}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

You can't modify the caller's references, but something like this may be what you are looking for -

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

Outputs

First: [1, 2, 3, 4, 5]
Second: [6, 7, 8]
First: [6, 7, 8]
Second: [1, 2, 3, 4, 5]
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Technically these two lines should be in the method "first = fList.toArray(new Integer[fList.size()]); second = sList.toArray(new Integer[sList.size()]);" ;) as 1. see the method name; 2. they are finishing the job started in the method. Anyway, I guess that's what the teacher wants. – peter.petrov Jan 14 '14 at 19:43
0

OK, you cannot acctually switch the arrays (since it is impssible in Java), but you can switch their content. Lets say if the arrays doesnt have same length, you want to swich maximum possible amount of elements:

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

so with this switching:

First:  [1, 2, 3, 4, 5]
Second: [6, 7, 8]
First:  [6, 7, 8, 4, 5]
Second: [1, 2, 3]
kajacx
  • 12,361
  • 5
  • 43
  • 70