ARRAYLIST AND VECTOR
List Class ArrayList and class Vector belong to List interface. List is sub class of Collections. Collections Class provides static methods that manipulate collections polymorphically. A List (sometimes known as sequence) is an ordered Collection that can contain duplicate elements. List indices are zero based. Class ArrayList and Vector are resizable-array implementation of List.
With various methods available in the ArrayList class, empty list can be created, elements can be added, removed, accessed and we can know the size of the list. Similarly with Vector class, various methods are available to create list and manipulate it as necessary
Elements stored in ArrayList and Vector are objects of a subclass, whose superclass is Object class. Elements loose all type information. Thus, before elements can be used, they must be cast first to a correct type
Autoboxing occurs when primitive-type values are inserted into the list of type ArrayList or Vector ArrayList and Vector classes are located in java.util. Hence they must first be imported before the associated classes can be utilized in the Java program. To import ArrayList use import java.util.ArrayList;. To import class Vector use this; import java.util.Vector; or we can use import java.util.*;.
The ArrayList Class Similar to an array, an ArrayList allows object storage Unlike an array, an ArrayList object: Automatically expands when a new item is added Automatically shrinks when items are removed Requires: import java.util.ArrayList;
Creating and Using an ArrayList Create an ArrayList object with no-args constructor ArrayList nameList = new ArrayList(); To populate the ArrayList, use the add method: nameList.add("James"); nameList.add("Catherine"); To get the current size, call the size method nameList.size(); // returns 2
Creating and Using an ArrayList To access items in an ArrayList, use the get method nameList.get(1); In this statement 1 is the index of the item to get. Example: ArrayListDemo1.java
Using an ArrayList The ArrayList class's toString method returns a string representing all items in the ArrayList System.out.println(nameList); This statement yields : [ James, Catherine ] The ArrayList class's remove method removes designated item from the ArrayList nameList.remove(1); This statement removes the second item. See example: ArrayListDemo3.java
Using an ArrayList The ArrayList class's add method with one argument adds new items to the end of the ArrayList To insert items at a location of choice, use the add method with two arguments: nameList.add(1, "Mary"); This statement inserts the String "Mary" at index 1 To replace an existing item, use the set method: nameList.set(1, "Becky"); This statement replaces “Mary” with “Becky” See example: ArrayListDemo4.java
Using an ArrayList An ArrayList has a capacity, which is the number of items it can hold without increasing its size. The default capacity of an ArrayList is 10 items. To designate a different capacity, use a parameterized constructor: ArrayList list = new ArrayList(100);
Using a Cast Operator with the get Method An ArrayList object is not typed To retrieve items from an ArrayList, you must cast the item to the appropriate type ArrayList nameList = new ArrayList(); nameList.add("Mary"); // Inserts an item String str = (String)nameList.get(0); Try get without the cast to see the effect. Example: ArrayListDemo6.java
Using ArrayList as a Generic Data Type You can create a type-safe ArrayList object by using generics. For example an ArrayList object for Strings: ArrayList<String> nameList = new ArrayList<String>(); The get method no longer requires casts to work. Example: GenericArrayListDemo1.java Example: GenericArrayListDemo2.java