90 likes | 263 Views
ArrayList Class. An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList tracks both its logical size (the number of elements that are currently in it) and its physical size (the number of cells available to store elements).
E N D
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList tracks both its logical size (the number of elements that are currently in it) and its physical size (the number of cells available to store elements). When a programmer creates an array list, its logical size is 0. When the programmer inserts elements into an array list, the list automatically updates its logical size and adds cells to accommodate new objects if necessary. The list’s logical size is also updated when an element is removed.
ArrayList cont… The programmer must use methods rather than the subscript operator [] to manipulate elements in an array list. The positions available for access in an array list range from 0 to its logical size minus 1. Must include the following import: import java.util.ArrayList;
Advantages of an ArrayList over an Array An ArrayList shrinks and grows as needed in a program, whereas an array has a fixed length that is set when the array is created. In an ArrayList list, the last slot is always list.size() – 1, whereas in a partially filled array, you, the programmer, must keep track of the last slot currently in use. For an ArrayList, you can do insertion or deletion with just a single statement. Any shifting of elements is handled automatically. In an array, however, insertion or deletion requires you to write the code that shifts the elements.
Elements of an ArrayList The elements in an ArrayList must all be objects. Thus, the following attempt to declare and create an ArrayList of int fails with a syntaz error: ArrayList<int> list = new ArrayList<int>(); //Invalid (syntax error) ArrayLists cannot contain primitive types
Wrapper Classes Allows us to store primitive data types in array lists. A wrapper class is a class that contains a value of a primitive type. Values of the types boolean, char, int and double can be stored in objects of the wrapper classes Boolean, Character, Integer, and Double respectively. In order to use a primitive data type with an ArrayList you must use the appropriate wrapper class name as the element type specifier when declaring the ArrayList variable and instantiating the list.
Test an ArrayList of integers import java.util.ArrayList; public class TestArrayList{ public static void main(String [] args){ //Create a list of Integers ArrayList<Integer> list = new ArrayList<Integer>(); //Add the ints 1-100 to the list for (inti = 0; i < 100; i++) list.add(i); //Increment each int in the list for (inti = 0; i < list.size(); i++) list.set(i, list.get(i) + 1); //Display the contents of the list for (inti = 0; i < 100; i++) System.out.println(list.get(i)); } }
Enhanced for loop/for-each loop Used to iterate over an array or collection(ArrayList) General Form for(SomeType element : collection) { statements }
Enhanced for loop example public class Enhanced { public static void main(String [] args) { //declaration and initialization of array int[] array = {5, 4, 6, 3, 2, 9}; //outputs all elements of array for(int x : array) System.out.print(x + “ – “); } } //output 5 – 4 – 6 – 3 – 2 – 9 –