199k views
4 votes
Given an array, x of references to objects that implement the Comparable interface, two int variables k and j that contain valid indices to x, write some statements that exchange (or swap) the contents of the elements referenced by the integers.

1 Answer

5 votes

Answer:

Following are the code to this question:

Comparable t; //defining interface variable t

t = x[k]; //use t variable that holds array value

x[k] = x[j]; //use array to store x[j] value

x[j] = t;//use x[j] to assign value in the t

Step-by-step explanation:

In the given question it is already defined " x" is an array that is the reference object, that defines the Comparable interface "t", and in the next line, two integer variable "k and j" is defined, that swap the values.

In the above code, the Comparable interface "t" is defined, which uses the t variable to store array x[k] value, and x[k] stores the x[j] value, and x[j] stores t value, and perform the swapping.

User SamSol
by
4.9k points