Upload
jolene-riley
View
36
Download
0
Tags:
Embed Size (px)
DESCRIPTION
COMP 110: Introduction to Programming. Tyler Johnson Mar 30, 2009 MWF 11:00AM-12:15PM Sitterson 014. Announcements. Program 3 has been graded. Questions?. Today in COMP 110. Review from last time Arrays in Classes & Methods Intro to Sorting Programming Demo. Arrays. - PowerPoint PPT Presentation
Citation preview
COMP 110: Spring 20094
Today in COMP 110
Review from last time
Arrays in Classes & Methods
Intro to Sorting
Programming Demo
COMP 110: Spring 20096
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!
COMP 110: Spring 20097
Array Details
Syntax for creating an array: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 pressurepressure = new int[100]; //allocate memory for the array to hold
100 ints
COMP 110: Spring 20098
Arrays
The array itself is referred to by a name “scores” or “vector” (in our examples)
0 1 2 3 4
68 73 57 102 94
Indices
the array scoresscores[3]
COMP 110: Spring 20099
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 arrayPet[] pets = new Pet[20];int sizeOfArray = pets.length; //sizeOfArray will have the value
20
You cannot change the value of length (it is final)
COMP 110: Spring 200910
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;
COMP 110: Spring 200911
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
COMP 110: Spring 200913
Arrays as Instance Variables
Arrays can be used as instance variables in classespublic class Pressure {
private double[] pressure;}
public class Course {
private Student[] enrolledStudents;}
COMP 110: Spring 200914
Arrays as Instance Variables
public class Pressure {
private double[] pressure;
//ask user for the number of pressure values and read them inpublic 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
COMP 110: Spring 200915
Arrays of Objects
Creating an array of objects does not initialize the individual objectsStudent[] students = new Student[35];
students[0].getGPA(); //run-time error, students[0] holds no object
Each object in the array must be instantiatedstudents[0] = new Student();students[1] = new Student();…students[34] = new Student();
Do this in a loop
COMP 110: Spring 200916
Student[] students = new Student[3];for(int i = 0; i < students.length; i++) {
students[i] = new Student();}
1045 2584 2836
Arrays of Objects
major
class
GPA
major
class
GPA
major
class
GPA
? ? ?students
Remember: The value of an object is a memory
address!
COMP 110: Spring 200917
Indexed Variables as Arguments
Indexed Variablescores[0], pressure[4], etc.
The same as using a regular variablepublic void printNum(int num) { System.out.println(num);}
public void doStuff() {
printNum(7); //prints 7 out to screen
int[] scores = { 15, 37, 95 };
for(int i = 0; i < scores.length; i++) { printNum(scores[i]); //prints the ith score out to screen }}
COMP 110: Spring 200918
Array Assignment & Equality
Arrays are objectsThe 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!//a.equals(b) is false as well!
COMP 110: Spring 200919
Comparing Arrays
To determine whether two arrays hold the same data, compare the two arrays element by element
Lab 7 equals method
COMP 110: Spring 200920
What is the Output?
int a = 7;int b = a; b = 8;
System.out.println("A: " + a);System.out.println("B: " + b);
A: 7
B: 8
Output
//a is not changed by this
COMP 110: Spring 200921
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] + "}");
a: {3, 5, 6}
b: {3, 5, 6}
Output
//b holds same memory address as a//we’re changing both b & a!
This is like giving the array two names (a & b)
COMP 110: Spring 200923
Copying Arrays
int[] a = {4, 5, 6};int[] b = new int[a.length]; //create a new array b
//copy a’s entries into bfor(int i = 0; i < b.length; b++) {
b[i] = a[i];}
COMP 110: Spring 200924
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);
}
a = 9
num = 9
Output
COMP 110: Spring 200925
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] + "}");
}
a: {4, 5, 6}
Output
COMP 110: Spring 200926
What is the Output?
public void changeArray(int[] array) {array[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] + "}");
}
a: {7, 5, 6}
Output
COMP 110: Spring 200927
What is the Output?
public void changeArray(int[] array) {array = new int[3];array[0] = 7;array[1] = 5;array[2] = 6;
}
public static void main(String[] args) {int[] a = {4, 5, 6};
changeArray(a);System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}");
}
a: {4, 5, 6}
Output
COMP 110: Spring 200928
Graphical Example
public void changeArray(int[] array) {array = new int[3];array[0] = 7;array[1] = 5;array[2] = 6;
}
public static void main(String[] args) {int[] a = {4, 5, 6};
changeArray(a);System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}");
}
4 5 6a
array 7 5 6
a: {4, 5, 6}
Output
COMP 110: Spring 200930
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
COMP 110: Spring 200932
Pseudocode
for i = 0 to array.length - 1Find the index s of the smallest element starting at index iSwap elements i & s in the array
2 3 7 9 6 4 8
i s
COMP 110: Spring 200933
Decomposition
Methodsint indexOfSmallest(int[] array, int startInd)• Return the index of the smallest element in array
starting at startInd
void swap(int[] array, int a, int b)• Swap the elements at indices a & b in array