Chapter 8 OneDimensional Arrays - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Chapter 8 OneDimensional Arrays

Description:

Assuming that a class Student was declared elsewhere a client ... SelectionSort Class part 2. int findMinimum (double[] A, int i, int size) { int j, min = 1; ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 30
Provided by: brucem94
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8 OneDimensional Arrays


1
Chapter 8One-Dimensional Arrays
  • Lecture Slides to Accompany
  • An Introduction to Computer Science Using Java
    (2nd Edition)
  • by
  • S.N. Kamin, D. Mickunas, E. Reingold

2
Chapter Preview
  • In this chapter we will
  • introduce the array as a structure for storing
    large amounts of data
  • discuss common array operations
  • introduce algorithms for searching and sorting
    arrays
  • show how multiple images can be painted from an
    array to use in programming simple animations

3
Array Declarations
  • Arrays contain a fixed number of variables of
    identical type
  • Array declaration and allocation are separate
    operations
  • Declaration examples
  • int counts
  • double scores
  • String studentNames

4
Array Allocation
  • Arrays are allocated using the Java new operator
  • The syntax is
  • new typesize
  • Examples
  • counts new int10
  • scores new double15
  • studentNames new String10

5
Array Organization
  • 0 1 2 3 4 5 6 7 8 9
  • counts
  • Each box is an int variable
  • The numbers on top are each variables subscript
    or index
  • An array of size 10 has subscripts 0 to 9

6
Array Subscripts
  • Arrays can contain any one type of value (either
    primitive values or references)
  • Subscripts are used to access specific array
    values
  • Examples
  • counts0 // first variable in counts
  • counts1 // second variable in counts
  • counts9 // last variable in counts
  • counts10 // error trying to access
  • // variable outside counts

7
Expressions as Subscripts
  • Array subscripts do not have to be constants
  • Array subscripts do need to be integer
    expressions that evaluate to valid subscript
    values for the current array allocation
  • Examples
  • countsi
  • counts2i
  • countsI/2

8
Array Initialization
  • Arrays can be initialized by giving a list of
    their elements
  • If your list contains n elements the subscripts
    will range from 0 to n 1
  • You do not need to allocate the array explicitly
    after it is initialized
  • Example
  • int primes
  • 2, 3, 5, 7, 11, 13, 17, 19, 23, 29

9
Initializing an Array of Strings
  • final Sring NAME
  • Sunday, Monday, Tuesday,
  • Wednesday, Thursday, Friday,
  • Saturday
  • // procedure that prints the day of week
  • public void printName (int day,
  • OutputBox out)
  • out.print(NAMEday 1)

10
(No Transcript)
11
Aliases
  • It is possible to have two different variables
    refer to the same array
  • When this happens these variables are called
    aliases
  • Creating aliases is not a good programming
    practice
  • Example of how it can happen
  • int A, B
  • B new int 10
  • A B

12
(No Transcript)
13
Loops and Array Processing
  • Initializes counts to 0, 10, 20, , 90
  • for (int i0 i lt 10 i)
  • countsi i 10
  • Prints the contents of counts using length
  • for (int i0 i lt counts.length i)
  • out.println(countsi)

14
Extra Capacity Array
  • Arrays cannot grow after they have been allocated
  • You can allocate more space than you believe your
    application will need
  • If you guess too low, you will still run out of
    space
  • You do not need to use all the elements in an
    array (but total computer memory is finite)

15
Searching
  • This loop terminates as soon as it finds the
    value 90 stored in grades
  • bool found false
  • int i 0
  • while (i lt size !found)
  • // 90 is not in grades0..gradesi - 1
  • if (gradesi 90)
  • found true
  • else
  • i

16
(No Transcript)
17
Processing Parallel Arrays
  • This loop counts the number of students whose
    performance improved from the first test to the
    second
  • int improved 0
  • for (int i 0 i lt size i)
  • if (grades1i lt grades2i)
  • improved

18
(No Transcript)
19
Arrays of Objects
  • Arrays of objects are declared in the same manner
    as arrays of primitive variables
  • Assuming that a class Student was declared
    elsewhere a client application could declare and
    allocate an array of 10 students using
  • Student students
  • students new Student10

20
Passing Arrays as Arguments
  • When an array is passed as an argument to a
    method, what is passed is a pointer to the array,
    not a new array (arrays are passed by reference)
  • This means that if the method makes changes to
    the array, these changes are still in effect when
    the method returns to its caller
  • This is not true for primitive values which are
    passed by value and not by reference
  • Method header example
  • public void read(Student students)

21
Selection Sort
  • Find the smallest element among the elements
    A0..An-1 and call it Amin
  • Swap A0 and Amin so A0 contains the
    smallest element and A1..An-1 not sorted
  • Now the smallest element among the elements
    A1..An-1 and call it Amin
  • Swap A1 and Amin so A1 contains the second
    smallest element and A2..An-1 not sorted
  • Proceed similarly for A3, A4, and so on

22
SelectionSort Class part 1
  • public class SelectionSort
  • public void selectionSort (double A, int
    size)
  • for (int i0 i lt size i)
  • // elements in A0..Ai1 are less than
  • // elements in Ai..Asize-1 and
  • // A0..Ai-1 are sorted
  • int min findMinimum(A, i, size)
  • swap(A, I, min)

23
SelectionSort Class part 2
  • int findMinimum (double A, int i, int size)
  • int j, min 1
  • for (j i 1 j lt size j)
  • // Amin lt all elements in A0..Aj1
  • if (Aj lt Amin) min j
  • return min
  • void swap (double A, int I, int j)
  • double temp Ai
  • Ai Aj
  • Aj temp

24
Insertion Sort
  • Iterate over subscripts 1 to n-1
  • At the ith step, shift the elements A0..Ai so
    that this part of the array is sorted
  • Note when the ith iteration begins elements
    A0..Ai 1 are already sorted, so the only
    shifting is that required to insert Ai into
    A0..Ai-1

25
InsertionSort Class
  • class InsertionSort
  • public void InsertionSort (double A, int
    size)
  • int i,j
  • for (int i1 i lt size i)
  • double Ai Ai
  • j j - 1
  • while (j gt 0 Aj gt Ai)
  • Aj 1 Aj
  • j--
  • Aj 1 Ai

26
Linear Search
  • int linearSearch (int A, int key)
  • int i
  • for (i 0 i lt A.length i)
  • // key not in A0..Ai - 1
  • if (Ai key)
  • return i
  • // key not in A
  • return 1

27
Searching
  • When searching for int values you can test for
    exact matches using (Ai key)
  • Comparing two double values for equality will not
    always give a correct result
  • A better comparison for doubles would be
    (Math.abs(Ai key) lt epsilon)
  • Epsilon should be as small a value as is
    acceptable for your application

28
Using Arrays in Animation
  • Image mouse new ImageNUMBER
  • Int sleepTime 1540, 240, 240, 240, 240, 240,
    240, 240,
  • 240, 240, 240, 240, 240,
    240, 240
  • public void show( )
  • for (int i 0 i lt NUMBER i)
  • mousei Toolkit.getDeafaultTool().
  • getImage(images/T(i1)gif)
  • ticker 0
  • do
  • g.drawImage(mouseticker, 70, 70)
  • Timer.pause(sleepTimeticker)
  • ticker (ticker 1) NUMBER
  • while (true)

29
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com