Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE205 Lecture 18

CSE205 Lecture 18

Object-Oriented Programming and Data Structures
Generics
(202204)

Javier Gonzalez-Sanchez
PRO

September 28, 2021
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 205
    Object-Oriented Programming and
    Data Structures
    Lecture 18: Generic-Data Types
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Midterm Exams

    View Slide

  3. jgs
    Previously …

    View Slide

  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13
    jgs
    Program.java

    View Slide

  5. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 14
    jgs
    Class MyList

    View Slide

  6. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15
    jgs
    Class MyNode

    View Slide

  7. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16
    jgs
    I introduce you a List, a Linked List

    View Slide

  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17
    jgs
    insertFirst()
    null
    head
    newNode

    View Slide

  9. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 18
    jgs
    insertAt()
    null
    head
    newNode

    View Slide

  10. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19
    jgs
    deleteFirst()
    null
    head

    View Slide

  11. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20
    jgs
    deleteLast()
    null
    head

    View Slide

  12. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21
    jgs
    get (index)
    null
    head

    View Slide

  13. jgs
    Generics
    Parametrized Data Types

    View Slide

  14. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 23
    jgs
    Definition
    § Generics or parameterized types.
    § The idea is to allow type (Integer, String, … etc., and user-defined types) to
    be a parameter to methods, classes, and interfaces.
    § Using Generics, it is possible to create classes that work with different data
    types.
    § An entity such as class, interface, or method that operates on a
    parameterized type is called a generic entity.

    View Slide

  15. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 24
    jgs
    Case 1
    class Test {
    T obj;
    Test(T obj) {
    obj = obj;
    }
    public T getObject() {
    return obj;
    }
    }
    class Main {
    public static void main(String[] args) {
    Test iObj = new Test(15);
    System.out.println(iObj.getObject());
    Test sObj = new Test("Hello World!");
    System.out.println(sObj.getObject());
    }
    }
    // we can not use primitives like int, char or double.

    View Slide

  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 25
    jgs
    Case 2
    public class GenericMethodTest {
    public static void printArray( E[] inputArray ) {
    for(int i=0; iSystem.out.print(""+ inputArray[i]);
    }
    System.out.println();
    }
    public static void main(String args[]) {
    Integer[] intArray = { 1, 2, 3, 4, 5 };
    Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
    Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
    System.out.println("Array integerArray contains:");
    printArray(intArray);
    System.out.println("\nArray doubleArray contains:");
    printArray(doubleArray);
    System.out.println("\nArray characterArray contains:");
    printArray(charArray);
    }
    }

    View Slide

  17. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 26
    jgs
    Case 3
    class Test {
    T obj1; // An object of type T
    U obj2; // An object of type U
    Test(T obj1, U obj2) {
    this.obj1 = obj1;
    this.obj2 = obj2;
    }
    public void print() {
    System.out.println(obj1);
    System.out.println(obj2);
    }
    }
    class Main {
    public static void main (String[] args) {
    Test obj = new Test(”ABC", 15);
    obj.print();
    }

    View Slide

  18. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 27
    jgs
    Generic Data Types

    View Slide

  19. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 28
    jgs
    Generic Data Types

    View Slide

  20. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 29
    jgs
    Generic Data Types

    View Slide

  21. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 30
    jgs
    Questions

    View Slide

  22. jgs
    CSE 205 Object-Oriented Programming and Data Structures
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2021
    Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University.
    They cannot be distributed or used for another purpose.

    View Slide