COMPUTER 2430 Object Oriented Programming and Data Structures I
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)
Container Methods Adding elements Deleting elements Searching targets Updating elements Displaying elements . . .
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 () }
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; . . .
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; . . .
How to Grow an Array? Make a New Larger Array!
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() . . .
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; }
Only works the first time! Correct? public void addStudent( Student student ) { if (numStudents == MAX_SIZE) grow(); studentList[numStudents ++] = student; } Only works the first time!
Correct! public void addStudent( Student student ) { // if (numStudents == MAX_SIZE) if (numStudents == studentList.length) grow(); studentList[numStudents ++] = student; }
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; }
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; }
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; }
Prog2
/** 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 ); . . .
public static void main(String [] args) { try . . . String command; while ( sc.hasNext() ) command = sc.next().toLowerCase(); switch (command) case "add": addMember(); break; }
Quiz 2 10 points 25 minutes