method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java

The solution for “method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java” can be found here. The following code will assist you in solving the problem.

public class Pair
{
private T first;
private T second;

public Pair(T firstElement, T secondElement)
{
first = firstElement;
second = secondElement;
}

public T getFirst() { return first; }

public T getSecond() { return second; }

public void swap()
{

T temp = first;
first = second;
second = temp;
}

public String toString() { return “(” + first + “, ” + second + “)”; }
}
public Pair swap() {
return new Pair(second, first);
}
public class PairDemo
{
public static void main(String[] args)
{
String[] names = { “Tom”, “Diana”, “Harry” };
Pair result = firstContaining(names, “a”);
System.out.println(result.getFirst());
System.out.println(“Expected: Diana”);
System.out.println(result.getSecond());
System.out.println(“Expected: 1”);
swap();
}

public static Pair firstContaining(
String[] strings, String sub)
{
for (int i = 0; i < strings.length; i++) { if (strings[i].contains(sub)) { return new Pair(strings[i], i);
}
}
return new Pair(null, -1);
}
}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in