ArrayIntList Interface vs. Implementation in CSE.123

ArrayIntList Interface vs. Implementation in CSE.123
Slide Note
Embed
Share

In the lecture slides for ArrayIntList in CSE.123 Spring 2025, the focus is on understanding the difference between interface and implementation, emphasizing the importance of making informed decisions when choosing between different implementations of the same interface. The slides also discuss the value of opinions, exposure to various viewpoints, and rationalization in decision-making within the context of programming and data structures.

  • ArrayIntList
  • Interface
  • Implementation
  • CSE.123
  • Programming

Uploaded on Apr 30, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. LEC 03: ArrayIntList CSE 123 Spring 2025 BEFORE WE START Talk to your neighbors: Did you eat breakfast today? If so, what? LEC 03 CSE 123 CSE 123 Instructor: Nathan Brunelle Arohan Ashar Neha Rohini Rushil Ido Zachary Sebastian Joshua Sean TAs: Implementing Data Structures; ArrayIntList Hayden Caleb Justin Heon Rashad Srihari Benoit Derek Chris Bhaumik Kuhu Kavya Cynthia Shreya Ashley Harshitha Kieran Marcus Crystal Eeshani Prakshi Packard Cora Dixon Nichole Questions during Class? Niyati Trien Lawrence Evan Cady Raise hand or send here Ziao sli.do #cse123A

  2. LEC 03: ArrayIntList CSE 123 Spring 2025 Announcements Programming Assignment 0 due Wed April 16 at 11:59pm! Looking ahead: - First resubmission opens Wed, April 16 - First quiz on Tue, April 22 (more detail next week) - C1 will have one (small) part demonstrating a feature of VSCode

  3. LEC 03: ArrayIntList CSE 123 Spring 2025 Lecture Outline Revisiting Reflections Interface v. Implementation Implementing ArrayList

  4. LEC 03: ArrayIntList CSE 123 Spring 2025 Revisiting Reflections Throughout this course, we ll ask you to form opinions on topics - Provide exposure to issues so you can decide for yourself Opinions aren t formed in a vacuum - Exposure to various viewpoints reinforces/challenges perspectives - Shouldn t be making arbitrary decisions - Rationalization is often important! (Not always necessary, but helps in communication) Integrating reflections to in-class components - Discuss opinions, challenge assumptions, potentially change minds - Please be respectful of other people s opinions - There are no right or wrong answers to these questions - Everyone has different experiences with the world that informs their decisions

  5. LEC 03: ArrayIntList CSE 123 Spring 2025 Lecture Outline Revisiting Reflections Interface vs. Implementation Implementing ArrayList

  6. LEC 03: ArrayIntList CSE 123 Spring 2025 Interface versus Implementation Interface: what something should do Implementation: how something is done These are different! Big theme of CSE 123: choose between different implementations of same interface

  7. LEC 03: ArrayIntList CSE 123 Spring 2025 Client versus Implementor uses Client Code Implementation interface Client Implementor

  8. LEC 03: ArrayIntList CSE 123 Spring 2025 Arrays vs. ArrayLists Arrays ArrayLists int[] arr = new int[x]; List<Integer> al = new ArrayList<>(); int y = arr[0] int y = al.get(0); - al.add(2); arr[0] = 5; al.set(0, 5); int length = arr.length; // Always x int size = al.size(); // Matches # of // things added Fundamental data structure Class within java.util Fixed length Illusion of resizing

  9. LEC 03: ArrayIntList CSE 123 Spring 2025 Implementing Data Structures No different from designing any other class! - Specified behavior (List interface): Method Description Adds the given value to the end of the list add(E value) Adds the given value at the given index add(int index, E value) Removes the given value if it exists remove(E value) Removes the value at the given index remove(int index) Returns the value at the given index get(int index) Updates the value at the given index to the one given set(int index, int value) Returns the number of elements in the list size() - Choose appropriate fields based on behavior Just requires some thinking outside the box

  10. LEC 03: ArrayIntList CSE 123 Spring 2025 Lecture Outline Revisiting Reflections Interface v. Implementation Implementing ArrayList

  11. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 al.add(2);

  12. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 al.add(2);

  13. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 2 0 0 0 0 0 0 0 0 0 al.add(2);

  14. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 2 0 0 0 0 0 0 0 0 0 al.add(5);

  15. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 2 0 0 0 0 0 0 0 0 0 al.add(5);

  16. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 2 5 0 0 0 0 0 0 0 0 al.add(5);

  17. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 2 5 0 0 0 0 0 0 0 0 al.add(-1);

  18. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 2 5 0 0 0 0 0 0 0 0 al.add(-1);

  19. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary 0 1 2 3 4 5 6 7 8 9 2 5 -1 0 0 0 0 0 0 0 al.add(-1);

  20. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary ? 0 1 2 3 4 5 6 7 8 9 2 5 -1 0 0 0 0 0 0 0 al.add(0, 0);

  21. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary ! 0 1 2 3 4 5 6 7 8 9 2 5 -1 0 0 0 0 0 0 0 al.add(0, 0);

  22. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary ! 0 1 2 3 4 5 6 7 8 9 2 5 -1 -1 0 0 0 0 0 0 al.add(0, 0);

  23. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary ! 0 1 2 3 4 5 6 7 8 9 2 5 5 -1 0 0 0 0 0 0 al.add(0, 0);

  24. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary ! 0 1 2 3 4 5 6 7 8 9 2 2 5 -1 0 0 0 0 0 0 al.add(0, 0);

  25. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary B) 0 1 2 3 4 5 6 7 8 9 0 2 5 -1 0 0 0 0 0 0 al.add(0, 0);

  26. LEC 03: ArrayIntList CSE 123 Spring 2025 ArrayIntLists For simplicity: only about storing ints (no type variables) How do we accomplish resizing magic trick? Two fields: - int[] elementData; // Where we store elements - int size; // Storage boundary Important points: - size represents how far the curtain is peeled back - Can t use a hardcoded value! - Starting value is always at index 0 - Adding to / removing from beginning requires shifting elements

  27. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) ? 0 1 2 3 4 5 6 7 8 9 0 2 5 -1 5 12 -9 7 21 -3 al.add(2);

  28. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) ! 0 1 2 3 4 5 6 7 8 9 0 2 5 -1 5 12 -9 7 21 -3 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10

  29. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) B) 0 1 2 3 4 5 6 7 8 9 0 2 5 -1 5 12 -9 7 21 -3 0 2 5 -1 5 12 -9 7 21 -3 0 0 1 2 3 4 5 6 7 8 9 10

  30. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) B) 0 1 2 3 4 5 6 7 8 9 0 2 5 -1 5 12 -9 7 21 -3 0 2 5 -1 5 12 -9 7 21 -3 0 0 1 2 3 4 5 6 7 8 9 10

  31. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) B) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 0 0 2 2 5 5 -1 -1 5 5 12 -9 12 -9 7 7 21 -3 21 -3 0 al.add(2);

  32. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) B) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 0 0 2 2 5 5 -1 -1 5 5 12 -9 12 -9 7 7 21 -3 21 -3 0 al.add(2);

  33. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) B) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 0 0 2 2 5 5 -1 -1 5 5 12 -9 12 -9 7 7 21 -3 21 -3 2 al.add(2);

  34. LEC 03: ArrayIntList CSE 123 Spring 2025 Capacity and Resizing Capacity = length of underlying array Size = number of user-added elements What happens if we run out of space? (size==capacity) - We make a new (bigger array) and copy things over - Another layer to the resizing illusion! In reality, we don t typically add a single spot - What happens if we add again?

Related


More Related Content