Java array cannot grow. In order to create a growable array we can use ArrayList or Vector.
ArrayList and Vector classes work the same way, except that the methods in the Vector class are synchronized, whereas methods in ArrayList are not.
ArrayList class works with only objects, not with primitive data types.
ArrayList class is a generic class and it takes the type of its elements as the type parameter.
To work with primitive values, declare an ArrayList of one of the wrapper classes. For example, use ArrayList<Integer> to work with int elements.
The following code fragment illustrates the use of the ArrayList class:
import java.util.ArrayList; /*ww w . j av a2 s . c o m*/ public class Main { public static void main(String[] args) { ArrayList<Integer> ids = new ArrayList<>(); int total = ids.size(); // total will be zero System.out.println("ArrayList size is " + total); System.out.println("ArrayList elements are " + ids); ids.add(new Integer(10)); // Adding an Integer object. ids.add(20); // Autoboxing ids.add(30); // Autoboxing total = ids.size(); // total will be 3 System.out.println("ArrayList size is " + total); System.out.println("ArrayList elements are " + ids); ids.clear(); total = ids.size(); // total will be 0 System.out.println("ArrayList size is " + total); System.out.println("ArrayList elements are " + ids); } }
The code above generates the following result.
Like arrays, ArrayList uses zero-based indexing. The first element of ArrayList has an index of zero.
The following code illustrates the use of a for loop to iterate through elements of an ArrayList.
import java.util.ArrayList; //from ww w .ja v a2s .c o m public class Main { public static void main(String[] args) { ArrayList<String> namelist = new ArrayList<String>(); // Add some names namelist.add("A"); namelist.add("B"); namelist.add("C"); // Get the count of names in the list int count = namelist.size(); for (int i = 0; i < count; i++) { String name = namelist.get(i); System.out.println(name); } namelist.remove("A"); count = namelist.size(); for (int i = 0; i < count; i++) { String name = namelist.get(i); System.out.println(name); } } }
The code above generates the following result.
The ArrayList class has an overloaded method named toArray():
Object[] toArray( ) <T> T[] toArray(T[ ] a)
The first method returns the elements of ArrayList as an array of Object. The second method takes an array of any type as argument.
The following code shows how to convert an ArrayList to an array.
import java.util.ArrayList; import java.util.Arrays; //w ww .j a va2 s. co m public class Main { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("Java"); al.add("SQL"); al.add("Data"); System.out.println("ArrayList:" + al); String[] s1 = new String[al.size()]; String[] s2 = al.toArray(s1); System.out.println("s1 == s2:" + (s1 == s2)); System.out.println("s1:" + Arrays.toString(s1)); System.out.println("s2:" + Arrays.toString(s2)); s1 = new String[1]; s1[0] = "hello"; // Store hello in first element s2 = al.toArray(s1); System.out.println("s1 == s2:" + (s1 == s2)); System.out.println("s1:" + Arrays.toString(s1)); System.out.println("s2:" + Arrays.toString(s2)); } }
The code above generates the following result.