Intro to Computer Science Arrays - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Intro to Computer Science Arrays

Description:

10. Creating, Initializing, and Accessing. One way to create an Array is with the new operator. ... SLAStudent itamar = new SLAStudent('Itamar Ben-Amos', 'blue' ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 19
Provided by: cat
Category:

less

Transcript and Presenter's Notes

Title: Intro to Computer Science Arrays


1
Intro to Computer Science Arrays
  • Instructor Ms. Catherine Stocker
  • Teaching Assistants Alex, Katie, Siraaj,
    Isaiah, Allison, Thibault
  • University of Pennsylvania
  • 26 March 2008

1
2
Todays Agenda
  • Discussion (Midterm and Quizzes)
  • Arrays
  • Arrays Lab
  • Implementing Arrays

2
3
Discussion/Demo
  • Lets 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

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 int5
  • creates an array of size 5, that contains
    integers
  • These lines assign values to each element of the
    array
  • data0 100
  • data1 200
  • data2 300
  • data3 400
  • data4 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,
    500int x data0 data1int y data4
    - data3data2 data3int z x y

11
12
Accessing an Arrays Length
  • int datadata new int6
    data.length is 6
  • Why is length important?for(int i 0 i lt
    data.length i) datai 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
    ...

14
15
Sample Interactions
  • gt PennStudent isaiah new PennStudent("Isaiah
    Greene", "green")
  • gt SLAStudent itamar new SLAStudent("Itamar
    Ben-Amos", "blue")
  • gt Student students new Student2
  • gt students0 isaiah
  • Isaiah Greene is a Penn Student.
  • gt students0.getName()
  • "Isaiah Greene"
  • gt students1 itamar
  • Itamar Ben-Amos is an SLA Student.
  • gt students1.getName()
  • "Itamar Ben-Amos"
  • gt PennStudent pennStudents new PennStudent2
  • gt pennStudents0 isaiah
  • Isaiah Greene is a Penn Student.
  • gt pennStudents0.getName()
  • "Isaiah Greene"
  • gt pennStudents1 itamar
  • Error Bad types in assignment

15
16
Sample Interactions cont.
  • gt pennStudents0.changeColor("purple")
  • true
  • gt pennStudents0 null
  • null
  • gt pennStudents0
  • null
  • gt 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 int33 anArray00
100 anArray01 200 anArray02
300 anArray10 400 anArray11
500 anArray12 600 anArray20
700 anArray21 800 anArray22 900
Write a Comment
User Comments (0)
About PowerShow.com