Announcements Lab 7 due Wednesday Assignment 4 due Friday
Today in COMP 110 Review from last time Arrays in Classes & Methods Intro to Sorting Programming Demo
Arrays Review from last time Questions?
Arrays A special kind of object in Java used to store a collection of data What if you wanted to store 80 basketball scores? Instead of declaring 80 integer variables, declare a single array!
Array Details Syntax for creating an array: Example: Alternatively: Base_Type[] Array_Name = new Base_Type[Length]; Example: int[] pressure = new int[100]; //create 100 variables of type int that can //be referred to collectively Alternatively: int[] pressure; //declare an integer array called pressure pressure = new int[100]; //allocate memory for the array to hold 100 ints
Arrays The array itself is referred to by a name “scores” or “vector” (in our examples) Indices 1 2 3 4 68 73 57 102 94 the array scores scores[3]
Array Length An array is a special kind of object It has one public instance variable: length length is equal to the length of the array Pet[] pets = new Pet[20]; int sizeOfArray = pets.length; //sizeOfArray will have the value 20 You cannot change the value of length (it is final)
For Loops and Arrays For loops are often perfectly suited to processing arrays Why? Because we know the number of iterations (array.length) int[] pressure = new int[100]; for(int index = 0; index < pressure.length; index++) scores[index] = 0;
Be Careful with Indices Indices MUST be in bounds double[] entries = new double[5]; entries[5] = 3.7; Your code WILL compile with an out-of-bounds index But it will result in a run-time error (crash) //RUN-TIME ERROR! Index out of bounds
Arrays in Classes & Methods
Arrays as Instance Variables Arrays can be used as instance variables in classes public class Course { private Student[] enrolledStudents; } public class Pressure { private double[] pressure;
Arrays as Instance Variables public class Pressure { private double[] pressure; //ask user for the number of pressure values and read them in public void getData() { System.out.println("Hi. How many pressure values would you like to enter?"); Scanner keyboard = new Scanner(System.in); int num = keyboard.nextInt(); pressure = new double[num]; System.out.println("Ok. Please enter " + num + " integers"); for(int i = 0; i < pressure.length; i++) { pressure[i] = keyboard.nextDouble(); } } } An array instance variable Create storage for the array Write values into the array
Arrays of Objects Creating an array of objects does not initialize the individual objects Student[] students = new Student[35]; students[0].getGPA(); //run-time error, students[0] holds no object Each object in the array must be instantiated students[0] = new Student(); students[1] = new Student(); … students[34] = new Student(); Do this in a loop
Arrays of Objects 1045 2584 2836 major class GPA major class GPA major Student[] students = new Student[3]; for(int i = 0; i < students .length; i++) { students[i] = new Student(); } 1045 2584 2836 students ? ? ? major class GPA major class GPA major class GPA
Indexed Variables as Arguments The same as using a regular variable public void printNum(int num) { System.out.println(num); } public void doStuff() { int[] scores = { 15, 37, 95 }; for(int index = 0; index < scores.length; index++) { printNum(scores[index]);
Array Assignment & Equality Arrays are objects The value of an array is a memory address Using == to compare arrays two arrays a & b returns whether they point to the same memory address int[] a = {4, 5, 6}; int[] b = {4, 5, 6}; //a == b is false!
Comparing Arrays To determine whether two arrays hold the same data, compare the two arrays element by element Lab 7 equals method
What is the Output? int a = 7; int b = a; b = 8; System.out.println("A: " + a); System.out.println("B: " + b); //a is not changed by this Output A = 7 B = 8
This is like giving the array two names (a & b) What is the Output? int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); System.out.println("b: {" + b[0] + ", " + b[1] + ", " + b[2] + "}"); This is like giving the array two names (a & b) //b holds same memory address as a //we’re changing both b & a! Output a = {3, 5, 6} b = {3, 5, 6}
Array Assignment 4 5 6 int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; a 3
Copying Arrays int[] a = {4, 5, 6}; int[] b = new int[a.length]; //create a new array b //copy a’s entries into b for(int i = 0; i < b.length; b++) { b[i] = a[i]; }
What is the Output? public void changeNumber(int num) { num = 7; } public static void main(String[] args) { int a = 9; changeNumber(a); System.out.println("a = " + a); int num = 9; changeNumber(num); System.out.println("num = " + num); Output a = 9 num = 9
What is the Output? public void changeNumber(int num) { num = 7; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeNumber(a[0]); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); Output a = {4, 5, 6}
What is the Output? public void changeArray(int[] array) { a[0] = 7; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeArray(a); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); Output a = {7, 5, 6}
What is the Output? public void changeArray(int[] a) { a = new int[3]; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeArray(a); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); Output a = {4, 5, 6}
Programming Demo Sorting (Selection Sort)
Introduction to Sorting Given an array of numbers, sort the numbers into ascending order Input array: Sorted array: 4 7 3 9 6 2 8 2 3 4 6 7 8 9
Selection Sort 4 7 3 9 6 2 8 2 7 3 9 6 4 8 2 3 7 9 6 4 8 …
Pseudocode for i = 0 to array.length - 1 2 3 7 9 6 4 8 Find the index s of the smallest element starting at index i Swap elements i & s in the array 2 3 7 9 6 4 8 i s
Programming Demo Programming
Wednesday Multi-Dimensional Arrays