Upload
felix-moody
View
214
Download
0
Embed Size (px)
Citation preview
1
Intro to Computer Science Arrays
Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault
University of Pennsylvania
26 March 20081
2
Today’s Agenda
• Discussion (Midterm and Quizzes)• Arrays
• Arrays Lab• Implementing Arrays
2
3
Discussion/Demo
Let’s talk about the Midterm and Quizzes
3
4
What if we wanted to store the names of all players on a team?
• public class Team{
public String player1;
public String player2;
public String player3;
public String player4;
public String player5;
public String player6;
public String player7;
public String player8;
public String player9;
public String player10;
public String player11;
. . .4
5
... or to store the grades of all students in a course?
• public class Grades{
public String className;
public int grade1;
public int grade2;
public int grade3;
public int grade4;
public int grade5;
public int grade6;
public int grade7;
public int grade8;
public int grade9;
public int grade10;
public int grade11; public int grade12; public int grade13; public int grade14; public int grade15;
. . .
5
6
... or to keep track of all 2,496 of your facebook friends?
• public class FacebookAccount{
public String name;
public Person friend1;
public Person friend2;
public Person friend3;
public Person friend4;
public Person friend5;
public Person friend6;
public Person friend7;
public Person friend8;
public Person friend9;
public Person friend10;
public Person friend11; public Person friend12; public Person friend13; public Person friend14; public Person friend15; public Person friend16; public Person friend17; public Person friend18; public Person friend19; public Person friend20; public Person friend21; public Person friend22;
. . .6
7
Arrays
• An array is a container object that holds a fixed number of values of a single type.
• The length of an array is established when the array is created. After creation, its length is fixed.
• Each item in an array is called an element, and each element is accessed by its numerical index.
8
Counting in Java
• Array indices always start at 0!• There is a such thing as a 0th element.• You can only count up to array length - 1.• Anything else is “Out of Bounds.”• While using arrays, you will see a lot of these:• ArrayIndexOutOfBoundsException:
at java.lang.reflect.Array.get(Native Method)• An Array of size 15 has what indices?
8
9
Declaring a Variable to Refer to an Array
• An array's type is written as type[], where type is the data type of the contained elements.– int[] data; // declares an array of integers.
• Similarly, you can declare arrays of other types:– double[] prices;– boolean[] verdicts;– char[] grades;– String[] names;
9
10
Creating, Initializing, and Accessing
• One way to create an Array is with the new operator.– int[] data = new int[5];
• creates an array of size 5, that contains integers
• These lines assign values to each element of the array:– data[0] = 100;– data[1] = 200;– data[2] = 300;– data[3] = 400;– data[4] = 500;
• *NOTE: An Array of Size 5 has indices 0 through 4.
• This syntax creates and initializes an array all at once.– int[] data = {100, 200, 300, 400, 500};
10
11
Using Array Elements
• An “element” of an array of ints can be used just like other integers.
• The same goes for arrays of other types.
• What is the value of ‘z’ after we execute these lines?
int[] data = {100, 200, 300, 400, 500};int x = data[0] + data[1];int y = data[4] - data[3];data[2] = data[3];int z = x + y;
11
12
Accessing an Array’s Length
• int[] data;data = new int[6];
data.length is 6
• Why is length important?
for(int i = 0; i < data.length; i++){ data[i] = i + 2}
12
13
Arrays Of Objects
• Remember the Student Array Game!• PennStudents and SLAStudents are both
allowed into an Array of Students.• But SLAStudents are not allowed into an Array
of PennStudents (and vice versa)• You can perform operations and methods on the
elements of the array.• Changes to the state of an Object while it is in
the array remain even if the Object is removed.• The default value of an Object is null!
13
14
Example: Student, PennStudent, SLAStudent
• public abstract class Student{ abstract String getName(); abstract String getColor(); abstract boolean changeColor();}
• public class PennStudent extends Student{ ...}
• public class SLAStudent extends Student{ ...} 1
4
15
Sample Interactions• > PennStudent isaiah = new PennStudent("Isaiah Greene", "green")
• > SLAStudent itamar = new SLAStudent("Itamar Ben-Amos", "blue");
• > Student[] students = new Student[2]
• > students[0] = isaiah
• Isaiah Greene is a Penn Student.
• > students[0].getName()
• "Isaiah Greene"
• > students[1] = itamar
• Itamar Ben-Amos is an SLA Student.
• > students[1].getName()
• "Itamar Ben-Amos"
• > PennStudent[] pennStudents = new PennStudent[2]
• > pennStudents[0] = isaiah
• Isaiah Greene is a Penn Student.
• > pennStudents[0].getName()
• "Isaiah Greene"
• > pennStudents[1] = itamar
• Error: Bad types in assignment 15
16
Sample Interactions cont.
• > pennStudents[0].changeColor("purple")
• true
• > pennStudents[0] = null
• null
• > pennStudents[0]
• null
• > isaiah.getColor()
• "purple"
16
17
2D Array Tic Tac Toe
0 1 2
0 X O X
1 X O O
2 O X X
18
2D Arrays
int[][] anArray = new int[3][3];
anArray[0][0] = 100;
anArray[0][1] = 200;
anArray[0][2] = 300;
anArray[1][0] = 400;
anArray[1][1] = 500;
anArray[1][2] = 600;
anArray[2][0] = 700;
anArray[2][1] = 800;
anArray[2][2] = 900;