COMPUTER 2430 Object Oriented Programming and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I 1
Container Classes • Class Integers. List of Lab 1 • Class Scores. List of Prog 1 • Class Golf. League of Prog 2 • How to store multiple values? – Arrays – Linked Lists (CS 2630 – OOPS II) 2
Container Methods • • • Adding elements Deleting elements Searching targets Updating elements Displaying elements. . . 3
public class Student. List { private final int MAX_SIZE = 450; private Student[] student. List = new Student[MAX_SIZE]; private int num. Students = 0; public boolean add. Student( Student student ). . . public boolean remove. Student( Student student ). . . private int find ( Student student ). . . @Override public String to. String (). . . } 4
public class Student. List { private final int MAX_SIZE = 450; private Student[] student. List = new Student[MAX_SIZE]; private int num. Students = 0; public boolean add. Student( Student student ) { if (num < MAX_SIZE) { student. List[num. Students ++] = student; return true; } return false; }. . . } 5
What if we want to store more than 450 students? public class Student. List { private final int MAX_SIZE = 450; private Student[] student. List = new Student[MAX_SIZE]; private int num. Students = 0; public boolean add. Student( Student student ) { if (num < MAX_SIZE) { student. List[num. Students ++] = student; return true; } return false; }. . . } 6
How to Grow an Array? Make a New Larger Array! 7
public class Student. List { private final int MAX_SIZE = 450; private Student[] student. List = new Student[MAX_SIZE]; private int num. Students = 0; public void add. Student( Student student ) { if (num. Students == MAX_SIZE) grow(); student. List[num. Students ++] = student; } private void grow(). . . } 8
public class Student. List { private final int MAX_SIZE = 450; private Student[] student. List = new Student[MAX_SIZE]; private int num. Students = 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 < num. Students; i ++) list[i] = student. List[i]; // Point to the new array, since student. List is // the class field and all methods use it. student. List = list; } } 9
Correct? public void add. Student( Student student ) { if (num. Students == MAX_SIZE) grow(); student. List[num. Students ++] = student; } Only works the first time! 10
Correct! public void add. Student( Student student ) { // if (num. Students == MAX_SIZE) if (num. Students == student. List. length) grow(); student. List[num. Students ++] = student; } 11
Correct? private void grow () { int size = MAX_SIZE + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < num. Students; i ++) list[i] = student. List[i]; student. List = list; } 12
Correct! private void grow () { int size = student. List. length + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < num. Students; i ++) list[i] = student. List[i]; student. List = list; } 13
Method arraycopy private void grow () { int size = student. List. length + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < num. Students; i ++) list[i] = student. List[i]; // System. arraycopy(student. List, 0, // list, 0, num. Students); // DO NOT USE arraycopy! student. List = list; } 14
Prog 2 15
/** 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("Prog 2_1. in") ); } catch (Exception ex) { sc = new Scanner( System. in ); }. . . 16 }
public static void main(String [] args) { try. . . String command; while ( sc. has. Next() ) { command = sc. next(). to. Lower. Case(); switch (command) { case "add": add. Member(); break; . . . } } } 17
Quiz 2 10 points 25 minutes 18
- Slides: 18