In lecture today, we reviewed the ArrayList
class and how to be clients of that data structure. We talked about how ArrayList
is a generic class because you can create ArrayList
s to hold different types of data. In class we talked about ArrayList<String>
but you could put any object type between the <>
(e.g. ArrayList<Scanner>
, ArrayList<Random>
, even ArrayList<ArrayList<String>>
!).
This idea of generic structures should be familiar to you since we have seen this before when we first learned arrays. In Java, you can have an array of String (String[]
), array of scanners (Scanner[]
), or even an array of ints (int[]
); it's the exact same idea with slightly different syntax.
What if we wanted to make an ArrayList
that held ints? Your first guess would follow the example syntax from the arrays and say ArrayList
, but this unfortunately does not work! There is a subtle difference between the generics of structures like ArrayList
and arrays: generics for structure