Using It

Now that the constructor problem is solved, our generic DynamicArray compiles. Let’s use it.

To use a generic class, the caller names the element type in angle brackets:

DynamicArray<Student> roster = new DynamicArray<>();

roster.add(new Student(1, "Ada", 3.9));
roster.add(new Student(2, "Linus", 3.4));

Student first = roster.get(0);

The <Student> on the left says “this is a dynamic array of students.” The empty <> on the right (the “diamond”) lets Java infer the same type without us repeating it. From here on, the compiler knows roster.get(0) returns a Student, and it will not let us add anything that is not one.