Define an integer array ‘arr’ with size 2 in the main method and initialize the two elements in ‘arr’ to be 1 and 2
Write a method ‘swap’ and pass the ‘arr’ into the method, swap two elements of the ‘arr’ in the method
Print out all elements in ‘arr’ in the main method
System.out.print(arr[0] + ‘ ‘ + arr[1]);
Swap(arr);
System.out.print(arr[0] + ‘ ‘ + arr[1]);

public class ArrayInt
{
    public static void main (String[] args)
    {
        int [] arr = new int [2];

        arr [0]  = 1;
        arr [1] = 2;

        //System.out.println(arr[0] + " " + arr[1]);
    }

    public void swap (int [] arr)
    {
        for (int tmp : arr)
        {
            arr [0] = arr[1];
            arr[1] = tmp;
        }
        System.out.println(arr[0] + " " + arr[1]);

    }


}

When you do a swap, you should not use the for(int tmp : arr) or any for-loop because it is ambiguous and won't work with many other cases. What you should do is to directly access the array using array indices.

In your case, you have array size of 2 which means there are 0 and 1 indices for the array. In order to swap elements of array (0 and 1 indices), you need a temporary place holder to hold it. Think of it as below...

/*
Original array values,
want to swap these 2
+--------+    +--------+
| item 1 |    | item 2 |
+--------+    +--------+
  index0        index1


copy the item 1 to a temporary place holder (temporary has value of item 1)

+--------+    copy    +-----------+
| item 1 | ---------> | temporary |
+--------+            +-----------+
  index0

copy the value of item 2 to item 1,
now item 1 has the value of item 2 (both have the same value)

+--------+    copy    +--------+
| item 2 | ---------> | item 1 |
+--------+            +--------+
  index1                index0

copy the value of the temporary bac to item 2,
now item 2 has the value of item 1 and item 1 has the value of item 2
(item values are swapped)

+-----------+    copy    +--------+
| temporary | ---------> | item 2 |
+-----------+            +--------+
                           index1
*/

Edited 4 Years Ago by Taywin

I did that but i dont think its copied, here's the code

public class ArrayInt
{
    public static void main (String[] args)
    {
        int [] arr = new int [2];

        arr [0]  = 1;
        arr [1] = 2;

        System.out.println(arr[0] + " " + arr[1]);

    }

    public static void swap (int [] arr)
    {
         int tmp = arr[0];

            arr[1] = arr[0];
            tmp = arr[1];
        System.out.println(arr[0] + " " + arr[1]);



    }


}

What you did is opposite from what I wrote. Yours would look like...

/*
copy the item 1 to a temporary place holder (temporary has value of item 1)
[CORRECT]
+--------+    copy    +-----------+
| item 1 | ---------> | temporary |
+--------+            +-----------+
  index0

[INCORRECT]
+--------+    copy    +--------+
| item 1 | ---------> | item 2 |
+--------+            +--------+
  index0                index1

[INCORRECT]    
+--------+    copy    +-----------+
| item 2 | ---------> | temporary |
+--------+            +-----------+
  index1
*/

Please look at what I wrote again in my previous post.

This question has already been answered. Start a new discussion instead.