Академический Документы
Профессиональный Документы
Культура Документы
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
An array can store any type of data but only one type of data at a time.
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
Array Size
Arrays are objects and provide a length constant that can be tested.
double[] temperatures = new double[25];
Array Size
Example: DisplayTestScores.java
9
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]
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
= = = =
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
To use the class, the import statement, import java.util.Array; must be used. Example: SortDemo.java
16
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.
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
20