For example look at this:
ArrayList<String> arrayList = new ArrayList<String>();
Could I use the <>s with a custom class? How do I use them?
For example look at this:
ArrayList<String> arrayList = new ArrayList<String>();
Could I use the <>s with a custom class? How do I use them?
You can put any class in the <>
as long as it fulfills the generic class's constraints:
ArrayList<MyClass> arrayList = new ArrayList<MyClass>();
You can define your own class to use the <>
simply:
class MyGenericClass<E> {
E e;
MyGenericClass(E e) { this.e = e; }
E getE() { return e; }
void setE(E e) { this.e = e; }
}
Now you can make your own:
MyGenericClass<String> stuff = new MyGenericClass<String>("Foo");
System.out.println(stuff.getE());
The tutorial Generics gives you all the information needed. The section Generic types shows how to declare and implement custom classes.
class MyClass<T> {
...
}