Вы находитесь на странице: 1из 16

Arrays - II

Math 130
Introduction to Computer Programming Lecture #20 Friday, October 12, 2007

B Smith: From Wu

Learning Outcomes
Understand and use basic Search and Sort algorithms Describe how a twodimensional array is implemented as an array of arrays Declare and use command line arguments Manipulate a collection of objects, using ArrayLists
3

Manipulate a collection of data values, using an array Declare and use an array of primitive data types in writing a program Declare and use an array of objects in writing a program Define a method that accepts an array as its parameter and a method that returns an array

Review: Array Basics


Primitive variables are designed to hold only one value at a time.

Arrays allow us to create a collection of like values that are indexed.

An array can store any type of data but only one type of data at a time.

An array is a list of data elements.

Administrivia
Project 2 grading revisited:

No points will be deducted if enums are not used, but half a letter grade in extra credit will be given if enums are used. half a letter grade if arrays are used
One full letter grade max extra credit possible

B Smith: fa07: covered in previous lecture at board

Array Size

Arrays are objects and provide a length constant that can be tested.
double[] temperatures = new double[25];

The length of this array is 25.

The length of an array can be obtained via its length constant.


int size = temperatures.length;

The variable size will contain 25.


8

B Smith: fa07: covered in previous lecture at board

Array Size

The length constant can be used in a loop to provide automatic bounding.


for(int i = 0; i < temperatures.length; i++){ System.out.println(Temperature + i : + temperatures[i]); } // Be careful of off-by-one errors.

It is possible to get the size of an array from a user:


int size; int[] numbers; System.out.print("How many numbers do you have? "); size = Keyboard.readInt(); numbers = new int[size];

Example: DisplayTestScores.java
9

B Smith: fa07: covered in previous lecture at board

Useful Array Operations

Summing Array Elements:


int total = 0; // Initialize accumulator for (int i = 0; i < units.length; i++) total += units[i];

Averaging Array Elements:


double total = 0; // Initialize accumulator double average; // Will hold the average for (int i = 0; i < scores.length; i++) total += scores[i]; average = total / scores.length;

Example: SalesData.java, Sales.java


10

String Arrays
Arrays are not limited to primitive data. An array of String objects can be created:
String[] names = { "Bill", "Susan", "Steven", "Jean" };

The names variable holds the address to the array. Address names[0]

A String array is an array of references to String objects.

address

Bill

names[1]
names[2] names[3]

address
address address

Susan
Steven Jean

Example: MonthDays.java

11

String Arrays
If an initialization list is not provided, the keyword new must be used to create the array: String[] names = new String[4];

The names variable holds the address to the array. Address names[0] names[1] names[2] names[3] null null null null
12

String Arrays
When an array is created in this manner, each element of the array must be initialized.
The names variable holds the address to the array. Address names[0] names[1] names[2] names[3] null null null null Bill Susan Steven Jean
13

names[0] names[1] names[2] names[3]

= = = =

"Bill"; "Susan"; "Steven"; "Jean";

Arrays of Objects
Since Strings are objects, we know that arrays can contain objects.
InventoryItem[] inventory = new InventoryItem[5];

The inventory variable holds the address of an InventoryItem array. Address inventory[0] inventory[1] inventory[2] inventory[3] inventory[4] null null null null null
14

Arrays of Objects
Each element needs to be initialized.
for (int i = 0; i < inventory.length; i++) inventory[i] = new InventoryItem();

Example: ObjectArray.java
The inventory variable holds the address of an InventoryItem array. Address inventory[0] Address inventory[1] Address inventory[2] Address inventory[3] Address inventory[4] Address
description: units: 0 description: units: 0 description: units: 0 description: units: 0 description: units: 0
15

Sorting an Array Using Array.Sort()


Java provides a class named Array that simplifies some array operations. The Array class has a static method named sort that will sort a numeric array in ascending order.
Array.sort(numbers);

To use the class, the import statement, import java.util.Array; must be used. Example: SortDemo.java
16

B Smith: discuss from animation and code

The Selection Sort Algorithm

A sort algorithm is a method of arranging data in a chosen order The Selection Sort algorithm uses a nested loop to:

sequentially step through an array multiple times, comparing elements to a chosen minimum value and swapping when
Smaller value is found

Example: SelectionSortDemo.java

17

Selection Sort
Selection sort idea:

find smallest element in the array and exchange it with the element in the first position. find second smallest element and exchange it with the element in the second position.

Do this (n-1) times.

input

4 4 1

2 2

9 9 4

7 7 5

8 8 7

5 5 7 8

1 1 4 9

find min
18

Selection Sort Method


public static void selectionSort(int[] array) { int startScan, index, minIndex, minValue; for (startScan = 0; startScan < (array.length-1); startScan++) { minIndex = startScan; minValue = array[startScan]; for(index = startScan + 1; index < array.length; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } }

20

Вам также может понравиться