Created
July 7, 2014 14:42
-
-
Save afaquejam/08b25b113dbd26354c4b to your computer and use it in GitHub Desktop.
Java Generics Example
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.ArrayList; | |
public class Generics<Items> { | |
private ArrayList<Items> container = new ArrayList<Items>(); | |
public void fillContainer(Items item) { | |
container.add(item); | |
} | |
public void displayContents() { | |
for(Items item: container) { | |
System.out.println(item); | |
} | |
} | |
public static void main(String[] arguments) { | |
Generics<Integer> integers = new Generics<Integer>(); | |
integers.fillContainer(3); | |
integers.fillContainer(4); | |
integers.displayContents(); | |
Generics<String> strings = new Generics<String>(); | |
strings.fillContainer("Hello"); | |
strings.fillContainer("World"); | |
strings.displayContents(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment