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

CSE205 Lecture 19

CSE205 Lecture 19

Object-Oriented Programming and Data Structures
Collections
(202110)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

September 29, 2021
Tweet

Transcript

  1. jgs CSE 205 Object-Oriented Programming and Data Structures Lecture 19:

    Collections Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 2 jgs

    Homework 04 This assignment is about implementing (from Scratch) the Classes MyNode, MyList, MyStack, and MyQueue. These classes should be Generic, i.e., use parametrized types. Implement the following methods: a) For MyList: instertFirst(), insertAt(), insertLast(), removeFirst(), removeAt(), removeLast(), printAll() b) For MyStack: push(), pop() c) For MyQueue: enqueue(), dequeue() Finally, create a Class Main with a method main() in which you a) create objects MyList, MyStack and MyQueue with data of type String. Insert, remove and print elements using your methods. Test all of them. b) reuse a class Student or Triangle or similar from previous assignments and create objects MyList, MyStack and MyQueue with data of that type. Insert, remove and print elements using your methods. Test all of them.
  3. jgs Collections

  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs

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

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

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

    Definition § Java Collections is a framework that provides an architecture to store and manipulate the group of objects. § Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8 jgs

    Hierarchy
  9. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9 jgs

    Common methods § public boolean add(E e) § public boolean remove(E e) § public int size() § public void clear() § public boolean contains(Object element) § public boolean isEmpty() § And more …
  10. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10 jgs

    The interface List § List <data-type> list1= new LinkedList(); § List <data-type> list4 = new Stack(); § LinkedList <data-type> list2 = new LinkedList(); § Stack <data-type> list4 = new Stack();
  11. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11 jgs

    LinkedList import java.util.*; public class TestJavaCollection2{ public static void main(String a []){ LinkedList<String> al=new LinkedList<String>(); al.add(”Mary"); al.add(”John"); al.add(”Tom"); al.add(”Susan"); } }
  12. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12 jgs

    Stack import java.util.*; public class TestJavaCollection4{ public static void main(String args[]){ Stack<String> stack = new Stack<String>(); stack.push(”Sun"); stack.push(”Earth"); stack.push(”Pluto"); stack.push(”Saturn"); System.out.println(stack.pop()); System.out.println(stack.pop()); System.out.println(stack.pop()); } }
  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs

    PriorityQueue // PriorityQueue doesn't allow null values to be stored in the queue import java.util.*; public class TestJavaCollection5{ public static void main(String args[]){ PriorityQueue<String> queue=new PriorityQueue<String>(); queue.add(”One"); queue.add(”Two"); queue.add(”Three"); queue.add(”Four"); // Retrieves, but does not remove, the head of this queue System.out.println("head:"+queue.element()); System.out.println("head:"+queue.peek()); // Retrieves and removes the head of this queue queue.remove(); queue.poll(); System.out.println("after removing two elements:"); }
  14. jgs Test Yourselves

  15. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs

    What about the classes § Student § Section § Course § AcademicProgram
  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16 jgs

    Questions
  17. jgs CSE 205 Object-Oriented Programming and Data Structures Javier Gonzalez-Sanchez,

    Ph.D. javiergs@asu.edu 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.