Exception Handling by e51r8Xi

VIEWS: 12 PAGES: 28

									Programming With Java




                            Generics and The ArrayList Class
       ICS201




                                               Lecture 13




               Dr. Jihene Kaabi   University Of Ha’il (Girls Branch)    Semester 081   1
Programming With Java
       ICS201
                                               Part 1




                             The ArrayList Class



               Dr. Jihene Kaabi   University Of Ha’il (Girls Branch)    Semester 081   2
Programming With Java
                                      The ArrayList Class
                        o ArrayList is a class in the standard Java libraries.

                            Unlike arrays, which have a fixed length once they
       ICS201




                                  have been created, an ArrayList is an object that

                                  can grow and shrink while your program is running.

                        o In general, an ArrayList serves the same purpose as

                          an array, except that an ArrayList can change length

                          while the program is running.




               Dr. Jihene Kaabi          University Of Ha’il (Girls Branch)    Semester 081   3
Programming With Java
                                  ArrayLists Disadvantages

                        • An ArrayList is less efficient than an array.

                        • It does not have the convenient square bracket notation.
       ICS201




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




               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   4
                                  Using the ArrayList Class
                        o In order to make use of the ArrayList class, it must first
Programming With Java



                        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
       ICS201




                        follows:
                         ArrayList<BaseType> aList = new ArrayList<BaseType>();

                        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<String> list = new ArrayList<String>(20);


               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   5
Programming With Java
                                  The ArrayList Methods

                        o The tools for manipulating arrays consist only of the

                        square brackets and the instance variable length.
       ICS201




                        o ArrayLists, however, come with a selection of powerful

                        methods that can do many of the things for which code

                        would have to be written in order to do them using arrays.




               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   6
Programming With Java
       ICS201                     The ArrayList Methods




                        Example:
                        ArrayList<Double> list1 = new ArrayList<Double>(30);
                        ArrayList<String> list2 = new ArrayList<String>();




               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   7
Programming With Java
       ICS201                     The ArrayList Methods




                        Example:
                        ArrayList<String> list = new ArrayList<String>();
                        list.add(“Simab”);
                        list.add(“Shahid”);
                        list.add(0,“Semester 081”);


               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   8
Programming With Java
       ICS201                     The ArrayList Methods




                        Example:
                        ArrayList<String> list = new ArrayList<String>();
                        int index = 1;
                        list.add(“Hi”);
                        list.add(“There”);
                         string S1 = list.set(index, “Hello”);
                        List.add(S1);
                        String S2 = list.get(index);

               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   9
                                             Example 01
                        • ArrayList<integer> list = new ArrayList<integer>();
Programming With Java



                            int index = 2;
                            list.add(1); // will add at index 0
                            list.add(2); // will add at index 1
       ICS201




                            index = 1;
                        •     integer i= list.set(index, 7);
                            System.out.println(i);


                        • Output :
                             2


                        • The set() function of ArrayList class set the index
                          with the given value and returned the value
                          previously stored at that index



               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   10
                                             Example 01
                        • ArrayList<integer> list = new ArrayList<integer>();
Programming With Java



                            int index = 2;
                            list.add(1); // will add at index 0
                            list.add(2); // will add at index 1
       ICS201




                            index = 1;
                        •     integer i= list.get(index);
                            System.out.println(i);


                        • Output :
                             2


                        • The get() function of ArrayList class returned the
                          returned the value stored at that index




               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   11
Programming With Java
       ICS201                     The ArrayList Methods




               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   12
Programming With Java
       ICS201                     The ArrayList Methods




               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   13
Programming With Java                           Example
                        • ArrayList<Integer> list = new ArrayList<Integer>
                          (20);
                              list.add(1);
                              list.add(2);
       ICS201




                              list.add(3);

                               int index = 1;
                               list.remove(index);
                               for(Integer i : list)
                                        System.out.println(i +" ");
                        Output:
                        1
                        3
                        In this code the remove function has removed the
                           value stored at index 1.


               Dr. Jihene Kaabi    University Of Ha’il (Girls Branch)    Semester 081   14
Programming With Java                              Example
                        • ArrayList<Integer> list = new ArrayList<Integer>
                          (20);
                              list.add(1);
                              list.add(2);
       ICS201




                              list.add(3);

                                  int index = 1;
                                  Integer i =list.remove(index);

                                            System.out.println(i +" ");
                        Output:
                        2




               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   15
Programming With Java                              Example
                        • ArrayList<Integer> list = new ArrayList<Integer>
                          (20);
                              list.add(1);
                              list.add(2);
       ICS201




                              list.add(3);

                                  list.clear();
                                     for(Integer i : list)
                                         System.out.println(i +" ");

                        Output:
                        It will not print any thing because the clear() will
                           delete all the elements of the list.




               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   16
Programming With Java
       ICS201                     The ArrayList Methods




               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   17
Programming With Java                           Example
                        • ArrayList<Integer> list = new ArrayList<Integer>
                          (20);
                              list.add(1);
                              list.add(2);
       ICS201




                              list.add(3);

                               int index ;
                               index = list.indexOf(2);
                           System.out.println(“Index of 2 is “ + index);
                        Output:
                        Index of 2 is 1




               Dr. Jihene Kaabi    University Of Ha’il (Girls Branch)    Semester 081   18
Programming With Java                           Example
                        • ArrayList<Integer> list = new ArrayList<Integer>
                          (20);
                              list.add(4);
                              list.add(2);
       ICS201




                              list.add(3);
                              list.add(4);

                               int index ;
                               index = list.lastIndexOf(4);
                           System.out.println(“Last Index of 4 is “ + index);
                        Output:
                        Index of 4 is 3




               Dr. Jihene Kaabi    University Of Ha’il (Girls Branch)    Semester 081   19
Programming With Java
       ICS201                     The ArrayList Methods




               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   20
Programming With Java
       ICS201                     The ArrayList Methods




               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   21
Programming With Java
       ICS201                     The ArrayList Methods




               Dr. Jihene Kaabi     University Of Ha’il (Girls Branch)    Semester 081   22
                        Why are Some Parameters of Type Base_Type
                                 and Others of type Object

                         o When looking at the methods available in the ArrayList
Programming With Java




                           class, there appears to be some inconsistency.
                             In some cases, when a parameter is naturally an
       ICS201




                              object of the base type, the parameter type is the
                              base type.
                             However, in other cases, it is the type Object.
                         o This 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.




               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   23
                 For-each Loop for ArrayList Objects
                        o The ArrayList class is an example of a collection class.
Programming With Java




                        o Starting with version 5.0, Java has added a new kind of for loop
                          called a for-each or enhanced for loop.
       ICS201




                            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<Integer> list = new ArrayList<Integer>(10);
                                   for (Integer element : list)
                                      element = 20 ;



               Dr. Jihene Kaabi           University Of Ha’il (Girls Branch)    Semester 081   24
                        Use trimToSize to Save Memory
                        o An ArrayList automatically increases its capacity when
Programming With Java




                          needed.

                            However, the capacity may increase beyond what a
       ICS201




                              program requires.

                            In     addition,       although           an         ArrayList      grows
                              automatically      when       needed,         it     does   not    shrink
                              automatically.

                        o If 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.



               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)        Semester 081        25
                              Example (ArrayList class)
                        import java.util.*;
                        class ArrayListDemo {
Programming With Java



                          public static void main(String[] args) {
                           ArrayList<String> list = new ArrayList<String> (20);
                           list.add("AB") ; list.add("CD") ; list.add("GH") ;
                           list.add("IJ") ; list.add("KL") ; list.add("MN") ;
       ICS201




                           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


               Dr. Jihene Kaabi            University Of Ha’il (Girls Branch)    Semester 081   26
                                Example (ArrayList class)
                        // Print the element at position index1 in the ArrayList
Programming With Java


                           System.out.println(" ");
                           int index1=11;
                                 String element = list.get(index1) ;
                                 System.out.println("The element at position " +" " +index1 +" "
                           +"is" +" " +element);
       ICS201




                                 // 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 CD is at position 1

               Dr. Jihene Kaabi          University Of Ha’il (Girls Branch)    Semester 081    27
                        Parameterized Classes and Generics
                        o The class ArrayList is a parameterized class.
Programming With Java




                        o It has a parameter, denoted by Base_Type, that can be
                          replaced by any reference type to obtain a class for
       ICS201




                          ArrayLists with the specified base type.
                        o Starting 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.




               Dr. Jihene Kaabi       University Of Ha’il (Girls Branch)    Semester 081   28

								
To top