method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java
public Pair<U,T> swap() {
return new Pair(second, first);
}
method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java
public Pair<U,T> swap() {
return new Pair(second, first);
}
method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java
public class PairDemo
{
public static void main(String[] args)
{
String[] names = { "Tom", "Diana", "Harry" };
Pair<String, Integer> 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<String, Integer> firstContaining(
String[] strings, String sub)
{
for (int i = 0; i < strings.length; i++)
{
if (strings[i].contains(sub))
{
return new Pair<String, Integer>(strings[i], i);
}
}
return new Pair<String, Integer>(null, -1);
}
}
method swap to the Pair class of that swaps the first and second elements value of the pair in generic Pair class in java
public class Pair<T>
{
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 + ")"; }
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us