chapter 8 one dimensional arrays l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 8 One-Dimensional Arrays PowerPoint Presentation
Download Presentation
Chapter 8 One-Dimensional Arrays

Loading in 2 Seconds...

play fullscreen
1 / 29

Chapter 8 One-Dimensional Arrays - PowerPoint PPT Presentation


  • 169 Views
  • Uploaded on

Chapter 8 One-Dimensional Arrays. Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold. Chapter Preview. In this chapter we will: introduce the array as a structure for storing large amounts of data

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 8 One-Dimensional Arrays' - rico


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 8 one dimensional arrays

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

chapter preview
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
array declarations
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;

array allocation
Array Allocation
  • Arrays are allocated using the Java new operator
  • The syntax is:

new type[size];

  • Examples:

counts = new int[10];

scores = new double[15];

studentNames = new String[10];

array organization
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 variable’s subscript or index
  • An array of size 10 has subscripts 0 to 9
array subscripts
Array Subscripts
  • Arrays can contain any one type of value (either primitive values or references)
  • Subscripts are used to access specific array values
  • Examples:

counts[0] // first variable in counts

counts[1] // second variable in counts

counts[9] // last variable in counts

counts[10] // error – trying to access

// variable outside counts

expressions as subscripts
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:

counts[i]

counts[2*i]

counts[I/2]

array initialization
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};

initializing an array of strings
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(NAME[day – 1]);

}

aliases
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;

loops and array processing
Loops and Array Processing
  • Initializes counts to 0, 10, 20, … , 90

for (int i=0; i < 10; i++) {

counts[i] = i * 10;

}

  • Prints the contents of counts using length

for (int i=0; i < counts.length; i++) {

out.println(counts[i]);

}

extra capacity array
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)
searching
Searching
  • This loop terminates as soon as it finds the value 90 stored in grades

bool found = false;

int i = 0;

while (i < size && !found) {

// 90 is not in grades[0]..grades[i - 1]

if (grades[i] == 90)

found = true;

else

i++;

}

processing parallel arrays
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 < size; i++) {

if (grades1[i] < grades2[i])

improved++;

}

arrays of objects
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 Student[10];

passing arrays as arguments
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) {

selection sort
Selection Sort
  • Find the smallest element among the elements A[0]..A[n-1] and call it A[min]
  • Swap A[0] and A[min] so A[0] contains the smallest element and A[1]..A[n-1] not sorted
  • Now the smallest element among the elements A[1]..A[n-1] and call it A[min]
  • Swap A[1] and A[min] so A[1] contains the second smallest element and A[2]..A[n-1] not sorted
  • Proceed similarly for A[3], A[4], and so on
selectionsort class part 1
SelectionSort Class – part 1

public class SelectionSort {

public void selectionSort (double[] A, int size) {

for (int i=0; i < size; i++) {

// elements in A[0]..A[i–1] are less than

// elements in A[i]..A[size-1] and

// A[0]..A[i-1] are sorted

int min = findMinimum(A, i, size);

swap(A, I, min);

}

}

selectionsort class part 2
SelectionSort Class – part 2

int findMinimum (double[] A, int i, int size) {

int j, min = 1;

for (j= i + 1; j < size; j++)

// A[min] <= all elements in A[0]..A[j–1]

if (A[j] < A[min]) min =j;

return min;

}

void swap (double[] A, int I, int j) {

double temp = A[i];

A[i] = A[j];

A[j] = temp;

}

}

insertion sort
Insertion Sort
  • Iterate over subscripts 1 to n-1
    • At the ith step, shift the elements A[0]..A[i] so that this part of the array is sorted
  • Note: when the ith iteration begins elements A[0]..A[i – 1] are already sorted, so the only shifting is that required to insert A[i] into A[0]..A[i-1]
insertionsort class
InsertionSort Class

class InsertionSort {

public void InsertionSort (double[] A, int size) {

int i,j;

for (int i=1; i < size; i++) {

double Ai = A[i];

j = j - 1;

while (j >= 0 && A[j] > Ai) {

A[j + 1] = A[j];

j--;

}

A[j + 1] = A[i];

}

}

}

linear search
Linear Search

int linearSearch (int[] A, int key)

int i;

for (i = 0; i < A.length; i++) {

// key not in A[0]..A[i - 1]

if (A[i] == key)

return i;

// key not in A

return –1;

}

searching27
Searching
  • When searching for int values you can test for exact matches using (A[i] == key)
  • Comparing two double values for equality will not always give a correct result
  • A better comparison for doubles would be (Math.abs(A[i] – key) < epsilon)
  • Epsilon should be as small a value as is acceptable for your application
using arrays in animation
Using Arrays in Animation

Image[] mouse = new Image[NUMBER];

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 < NUMBER; i++)

mouse[i] = Toolkit.getDeafaultTool().

getImage(“images/T”+(i+1)+”gif);

ticker = 0;

do {

g.drawImage(mouse[ticker}, 70, 70);

Timer.pause(sleepTime{ticker]);

ticker = (ticker + 1) % NUMBER;

} while (true);

}

}