Created
December 15, 2012 11:32
-
-
Save pfalabella/4293973 to your computer and use it in GitHub Desktop.
java implementation for http://stackoverflow.com/questions/13878944/how-to-handle-a-collection-of-foot-where-t-can-be-different-for-each-item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
import static java.lang.System.out; | |
class Main | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
List<Pair<?>> lst = new ArrayList<Pair<?>>(); | |
lst.add(new Pair<String>("a","b")); | |
lst.add(new Pair<Integer>(1,2)); | |
Application.SwapAll(lst); | |
for(Pair<?> pair: lst) out.println(pair); | |
} | |
} | |
class Pair<T> { | |
T First, Second; | |
public Pair(T First, T Second) { | |
this.First=First; | |
this.Second=Second; | |
} | |
public String toString() { | |
return String.format("(%s, %s)",First.toString(),Second.toString()); | |
} | |
} | |
class Application { | |
public static <T> void Swap(Pair<T> pair) { | |
T temp=pair.Second; | |
pair.Second = pair.First; | |
pair.First = temp; | |
} | |
public static void SwapAll(List<Pair<?>> lst) { | |
for(Pair<?> pair: lst) Application.Swap(pair); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment