-1

The following program swaps two elements in an array. But it does not seem to run, but I am sure that I am not doing anything wrong.

public class swap {

    public static String[] swap(int first, int second, String[] A) {
        String hold = A[first];
        A[first] = A[second];
        A[second] = hold;
        return A;
    }

    public static void main(String[] args) {
        String[] B = {"Dawn", "Justice", "Of"};
        String[] c = swap.swap(1, 2, B);
        System.out.print(c);
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
kunji mamu
  • 81
  • 1
  • 9

2 Answers2

0

1.) Need to create Object of swap class, or since it is static no need to create object, just use swap(1, 2, B);

2.) Print Arrays.toString(c)

Modified Code

public static String[] swap(int first, int second, String[] A) {
        String hold = A[first];
        A[first] = A[second];
        A[second] = hold;
        return A;

    }

    public static void main(String[] args) {
        String[] B = { "Dawn", "Justice", "Of" };
        String[] c = swap(1, 2, B);
        System.out.print(Arrays.toString(c));

    }

output

[Dawn, Of, Justice]
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
-1

import java.util.Arrays; public class swap {

public static String[] swap(int first, int second, String[] A) {
    String hold = A[first];
    A[first] = A[second];
    A[second] = hold;
    return A;
}

public static void main(String[] args) {
    String[] B = {"Dawn", "Justice", "Of"};
    String[] c = swap.swap(1, 2, B);
    System.out.print(Arrays.toString(c));
}

}