Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.

Similar presentations


Presentation on theme: "Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class."— Presentation transcript:

1 Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class

2 Programming With Java ICS201 University Of Ha’il2 Part 1 The ArrayList Class

3 Programming With Java ICS201 3 Lists list: an ordered sequence of elements, each accessible by a 0-based index –one of the most basic collections of data

4 Programming With Java ICS201 4 The ArrayList class Class ArrayList implements the notion of a list Think of it as an auto-resizing array when you want to use ArrayList, remember to import java.util.*;

5 Programming With Java ICS201 5 ArrayList vs. Array Array String[] names = new String[5]; names[0] = "Java"; String name = names[0]; ArrayList ArrayList namesList = new ArrayList (); namesList.add("Java"); String name = namesList.get(0);

6 Programming With Java ICS201 University Of Ha’il6 The ArrayList Class oArrayList is a class in the standard Java libraries.  Unlike arrays, which have a fixed length once they have been created, an ArrayList is an object that can grow and shrink while your program is running. oIn general, an ArrayList serves the same purpose as an array, except that an ArrayList can change length while the program is running.

7 Programming With Java ICS201 University Of Ha’il7 ArrayLists Disadvantages An ArrayList is less efficient than an array. It does not have the convenient square bracket notation. The base type of an ArrayList must be a class type (or other reference type): it cannot be a primitive type (int, double, or char).

8 Programming With Java ICS201 University Of Ha’il8 Using the ArrayList Class o In order to make use of the ArrayList class, it must first be imported from the package java.util. o An ArrayList is created and named in the same way as object of any class, except that you specify the base type as follows: ArrayList aList = new ArrayList (); o An initial capacity can be specified when creating an ArrayList as well.  e.g. The following code creates an ArrayList that stores objects of the base type String with an initial capacity of 20 items. ArrayList list = new ArrayList (20);

9 Programming With Java ICS201 9 Adding elements Elements are added dynamically to the list: ArrayList list = new ArrayList (); System.out.println("list = " + list); list.add("Red"); System.out.println("list = " + list); list.add("Blue"); System.out.println("list = " + list); list.add("Green"); System.out.println("list = " + list); Output: list = [] list = [Red] list = [Red, Blue] list = [Red, Blue, Green]

10 Programming With Java ICS201 10 Removing elements Elements can also be removed by index: System.out.println("before remove list = " + list); list.remove(0); list.remove(1); System.out.println("after remove list = " + list); Output: before remove list = [Tool, U2, Red, Green] after remove list = [U2, Green] –Notice that as each element is removed, the others shift downward in position to fill the hole. –Therefore, the second remove gets rid of Red, not U2. index0123 valueToolU2RedGreen index012 valueU2RedGreen index01 valueU2Green

11 Programming With Java ICS201 11 ArrayList methods Method nameDescription add(value)adds the given value to the end of the list add(index, value)inserts the given value before the given index clear()removes all elements contains(value)returns true if the given element is in the list get(index)returns the value at the given index indexOf(value)returns the first index at which the given element appears in the list (or -1 if not found) lastIndexOf(value)returns the last index at which the given element appears in the list (or -1 if not found) remove(index)removes value at given index, sliding others back size()returns the number of elements in the list

12 Programming With Java ICS201 12 ArrayList and for loop for ( : ) { ; } This syntax can be used to examine an ArrayList: int sum = 0; for (String s : list) { sum += s.length(); } System.out.println("Total of lengths = " + sum);

13 Programming With Java ICS201 University Of Ha’il13 The ArrayList Methods Example: ArrayList list1 = new ArrayList (10); ArrayList list2 = new ArrayList ();

14 Programming With Java ICS201 University Of Ha’il14 The ArrayList Methods Example: ArrayList list = new ArrayList (); int index = 2; …… list.set(index, "Here"); String S = list.get(index);

15 Programming With Java ICS201 University Of Ha’il15 The ArrayList Methods Example: ArrayList list = new ArrayList (); list.add("Java"); list.add("Course"); list.add(0, "Semester 201101");

16 Programming With Java ICS201 University Of Ha’il16 The ArrayList Methods

17 Programming With Java ICS201 University Of Ha’il17 The ArrayList Methods

18 Programming With Java ICS201 University Of Ha’il18 The ArrayList Methods

19 Programming With Java ICS201 University Of Ha’il19 The ArrayList Methods

20 Programming With Java ICS201 University Of Ha’il20 The ArrayList Methods

21 Programming With Java ICS201 University Of Ha’il21 The ArrayList Methods

22 Programming With Java ICS201 University Of Ha’il22 Why are Some Parameters of Type Base_Type and Others of type Object oWhen looking at the methods available in the ArrayList class, there appears to be some inconsistency.  In some cases, when a parameter is naturally an object of the base type, the parameter type is the base type.  However, in other cases, it is the type Object. oThis is because the ArrayList class implements a number of interfaces, and inherits methods from various classes.  These interfaces and classes specify that certain parameters have type Object.

23 Programming With Java ICS201 University Of Ha’il23 For-each Loop for ArrayList Objects oThe ArrayList class is an example of a collection class. oStarting with version 5.0, Java has added a new kind of for loop called a for-each or enhanced for loop.  This kind of loop has been designed to cycle through all the elements in a collection (like an ArrayList).  Syntax: for (Array_Base_Type Variable : ArrayList_Object) Statement  Example: ArrayList list = new ArrayList (10); for (Integer element : list) element = 20 ;

24 Programming With Java ICS201 University Of Ha’il24 Use trimToSize to Save Memory oAn ArrayList automatically increases its capacity when needed.  However, the capacity may increase beyond what a program requires.  In addition, although an ArrayList grows automatically when needed, it does not shrink automatically. oIf an ArrayList has a large amount of excess capacity, an invocation of the method trimToSize will shrink the capacity of the ArrayList down to the size needed.

25 Programming With Java ICS201 University Of Ha’il25 Example (ArrayList class) import java.util.*; class ArrayListDemo { public static void main(String[] args) { ArrayList list = new ArrayList (20); list.add("AB") ; list.add("CD") ; list.add("GH") ; list.add("IJ") ; list.add("KL") ; list.add("MN") ; list.add("QR") ; list.add("ST") ; list.add("WX") ; list.add("YZ") ; // Print the elements of the ArrayList System.out.println("The following is the initial ArrayList:"); for(String s : list) System.out.print(s +" "); // Add elements to the ArrayList list.add(2, "ef") ; list.add(7, "op") ; list.add(10, "uv") ; // Print the new ArrayList System.out.println(" "); System.out.println("The following is the new ArrayList:"); for(String s : list) System.out.print(s +" "); continued

26 Programming With Java ICS201 University Of Ha’il26 Example (ArrayList class) // Print the element at position index1 in the ArrayList System.out.println(" "); int index1=11; String element = list.get(index1) ; System.out.println("The element at position " +" " +index1 +" " +"is" +" " +element); // Print the index of an element of the ArrayList System.out.println(" "); int index = list.indexOf("CD") ; System.out.println("The element CD is at position " +" “ +index); } Output: The following is the initial ArrayList: AB CD GH IJ KL MN QR ST WX YZ The following is the new ArrayList: AB CD ef GH IJ KL MN op QR ST uv WX YZ The element at position 11 is WX The element BC is at position 1

27 Programming With Java ICS201 University Of Ha’il27 Parameterized Classes and Generics oThe class ArrayList is a parameterized class. oIt has a parameter, denoted by Base_Type, that can be replaced by any reference type to obtain a class for ArrayLists with the specified base type. oStarting with version 5.0, Java allows class definitions with parameters for types.  These classes that have type parameters are called parameterized class or generic definitions, or, simply, generics.


Download ppt "Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class."

Similar presentations


Ads by Google