Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Midterm Exams

Slide 3

Slide 3 text

jgs Previously …

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

jgs Generics Parametrized Data Types

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 25 jgs Case 2 public class GenericMethodTest { public static void printArray( E[] inputArray ) { for(int i=0; i

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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.