1 / 9

ArrayList Class

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).

Download Presentation

ArrayList Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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;

  3. 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.

  4. ArrayList Methods

  5. 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

  6. 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.

  7. 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)); } }

  8. Enhanced for loop/for-each loop Used to iterate over an array or collection(ArrayList) General Form for(SomeType element : collection) { statements }

  9. 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 –

More Related