Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 Container Classes Class IntegersList of Lab1 Class ScoresList of Prog1
Class GolfLeague of Prog2 How to store multiple values? Arrays Linked Lists (CS 2630 – OOPS II)

3 Container Methods Adding elements Deleting elements Searching targets
Updating elements Displaying elements . . .

4 public class StudentList
{ private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public boolean addStudent( Student student ) . . . public boolean removeStudent( Student student ) private int find ( Student student ) @Override public String toString () }

5 public class StudentList
{ private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public boolean addStudent( Student student ) if (num < MAX_SIZE) studentList[numStudents ++] = student; return true; } return false; . . .

6 What if we want to store more than 450 students?
public class StudentList { private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public boolean addStudent( Student student ) if (num < MAX_SIZE) studentList[numStudents ++] = student; return true; } return false; . . .

7 How to Grow an Array? Make a New Larger Array!

8 public class StudentList
{ private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public void addStudent( Student student ) if (numStudents == MAX_SIZE) grow(); studentList[numStudents ++] = student; } private void grow() . . .

9 public class StudentList
{ private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; private final int GROWBY = 50; private void grow () // Make a larger array int size = MAX_SIZE + GROWBY; Student[] list = new Student[size]; // Copy all existing elements for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; // Point to the new array, since studentList is // the class field and all methods use it. studentList = list; }

10 Only works the first time!
Correct? public void addStudent( Student student ) { if (numStudents == MAX_SIZE) grow(); studentList[numStudents ++] = student; } Only works the first time!

11 Correct! public void addStudent( Student student ) {
// if (numStudents == MAX_SIZE) if (numStudents == studentList.length) grow(); studentList[numStudents ++] = student; }

12 Correct? private void grow () { int size = MAX_SIZE + GROWBY;
Student[] list = new Student[size]; for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; studentList = list; }

13 Correct! private void grow () {
int size = studentList.length + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; studentList = list; }

14 Method arraycopy private void grow () {
int size = studentList.length + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; // System.arraycopy(studentList, 0, // list, 0, numStudents); // DO NOT USE arraycopy! studentList = list; }

15 Prog2

16 /** Uses a scanner to get input from a file and from System.in if the file unavailable. It reads in one command at a time until end of input. */ public static void main(String [] args) { try sc = new Scanner( new File("Prog2_1.in") ); } catch (Exception ex) sc = new Scanner( System.in ); . . .

17 public static void main(String [] args)
{ try . . . String command; while ( sc.hasNext() ) command = sc.next().toLowerCase(); switch (command) case "add": addMember(); break; }

18 Quiz 2 10 points 25 minutes


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google