COMP 110 More arrays - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 More arrays

Description:

Like a list of variables, but with a nice, compact way to name them ... Smiley[] smilies = new Smiley[3]; 14. 1045. 2584. 2836. true. GREEN. 3. false. BLUE. 1. false ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 23
Provided by: luv
Learn more at: http://www.cs.unc.edu
Category:
Tags: comp | arrays | more | smilies

less

Transcript and Presenter's Notes

Title: COMP 110 More arrays


1
COMP 110More arrays
  • Luv Kohli
  • November 7, 2008
  • MWF 2-250 pm
  • Sitterson 014

2
Announcements
  • Lab 6 due
  • Lab 7 assigned today
  • Final program, Program 4, assigned Monday
  • Electronic Arts (EA) information session
  • Tuesday, November 11, 6pm
  • Sitterson Hall 011
  • Free food!
  • Free games!

3
Questions?
4
Today in COMP 110
  • More about arrays
  • Start Lab 7

5
Review Arrays
  • An array is a collection of items of the same
    type
  • Like a list of variables, but with a nice,
    compact way to name them
  • A special kind of object in Java

6
Review Creating an array
  • int scores new int5
  • This is like declaring 5 strangely named
    variables of type int
  • scores0
  • scores1
  • scores2
  • scores3
  • scores4

7
Review Indexing
  • Variables such as scores0 and scores1 that
    have an integer expression in square brackets are
    known as
  • indexed variables, subscripted variables, array
    elements, or simply elements
  • An index or subscript is an integer expression
    inside the square brackets that indicates an
    array element

8
Review Indexing
  • Where have we seen the word index before?
  • Strings indexOf method
  • Index numbers start with 0. They do NOT start
    with 1 or any other number.

9
Review Indexing
  • The number inside square brackets can be any
    integer expression
  • An integer scores3
  • Variable of type int scoresindex
  • Expression that evaluates to int scoresindex3
  • Can use these strangely named variables just like
    any other variables
  • scores3 68
  • scores4 scores4 3 // just made a
    3-pointer!
  • System.out.println(scores1)

10
Review Array
  • The array itself is referred to by the name
    scores (in this particular case)

Indices
0 1 2 3 4
68 73 57 102 94
the array scores
scores3
11
Review Arrays
  • System.out.println("Enter 5 basketball scores")
  • int scores new int5
  • int scoreSum 0
  • for (int i 0 i lt 5 i)
  • scoresi keyboard.nextInt()
  • scoreSum scoresi
  • double average (double) scoreSum / 5
  • System.out.println("Average score " average)
  • for (int i 0 i lt 5 i)
  • if (scoresi gt average)
  • System.out.println(scoresi " above
    average")
  • else if (scoresi lt average)
  • System.out.println(scoresi " below
    average")
  • else

12
Review for-each
  • You can also use another form of the for loop
    with collections (such as arrays)
  • for (int s scores)
  • if (s gt average)
  • System.out.println(s " above
    average")
  • else if (s lt average)
  • System.out.println(s " below
    average")
  • else
  • System.out.println(s " equal to the
    average")
  • s takes on the value of each element of the array
    score, but you cannot change an elements value
    this way

13
Arrays as instance variables
  • public class Weather
  • private double temperature
  • private double pressure
  • public void initializeTemperature(int len)
  • temperature new doublelen

14
Arrays of objects
  • Smiley smilies new Smiley3

1045 2584 2836
true
GREEN
3
false
BLUE
1
false
CYAN
4
15
Arrays as arguments
  • public void changeArray(int arr)
  • int len arr.length
  • arrlen 1 25

23 47 52 14 7
25
16
Arrays as return types
  • public double buildArray(int len)
  • double retArray new doublelen
  • for (int i 0 i lt retArray.length i)
  • retArrayi i 1.5
  • return retArray

17
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
18
Selection sort pseudocode
  • for (index 0 index lt length index)
  • Find index of smallest value of array
  • between index and end of array
  • Swap values of current index and the
  • index with the smallest value

19
Selection sort
4 7 3 9 6 2 8
2 7 3 9 6 4 8
2 3 7 9 6 4 8
20
Bubble sort
  • Repeat until array is sorted

4 5 7 2 3 9 8
4 7 5 9 2 3 8
4 5 7 9 2 3 8
4 5 7 2 3 8 9
4 5 7 2 9 3 8
21
Lab 7
22
Monday
  • More arrays
Write a Comment
User Comments (0)
About PowerShow.com