The advantage of specifying the element type is that Java now knows what type of value the ArrayList contains. When you call set, Java can ensure that the value matches the element type.
Java ArrayList: Basic Use Full Documentation: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html How to declare an array list: ArrayList<String> list = new ArrayList<String>(); Any class name can be placed in the <>.
Let's write a class that implements a growable array of integers. ArrayList. toString() ... It will be very similar to the C++ Vector. The list's size will be the number of elements added to it so far. The actual array length ("capacity") in the object may be larger. We'll start with an array of length 10 by default.
When a list is created, it is initially empty. The list object keeps track of the element values that have been added to it, their order, indexes, and its total size. You can add, remove, get, set, ... any index at any time. examples found in the Java class libraries: (covered in this course!)
In this article from my free Java 8 course, I will be giving you a basic overview of the Java class java.util.ArrayList. I will first explain the meaning of size and capacity of an ArrayList and show you the difference between them.
When constructing an ArrayList, you must specify the type of elements it will contain between < and >. This is called a type parameter or a generic class. Allows the same ArrayList class to store lists of different types.