Skip to content

Instantly share code, notes, and snippets.

@afaquejam
Created July 7, 2014 14:42
Show Gist options
  • Save afaquejam/08b25b113dbd26354c4b to your computer and use it in GitHub Desktop.
Save afaquejam/08b25b113dbd26354c4b to your computer and use it in GitHub Desktop.
Java Generics Example
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