A Wide Array of Possibilities Javas Central Casting
A Wide Array of Possibilities Java’s Central Casting CS 102 -02 Lecture 3 -2 April 15, 1998 CS 102 -02 Lecture 3 -2
What’s an Array • An array is a group of values of the same type (usually stored in consecutive memory locations) • Arrays are objects – Can be assigned to variables of type Object • Dynamically created April 15, 1998 CS 102 -02 Lecture 3 -2
An Array of Examples From Figure 5. 3 int n[]; // declare an array of integers // initialize instance variables public void init() { n = new int[ 10 ]; //dynamic allocation } April 15, 1998 CS 102 -02 Lecture 3 -2
The Declaration of Array declarations int[] ai; short[][] as; Object[] ao, other. Ao; April 15, 1998 // array of int // array of short // array of Object CS 102 -02 Lecture 3 -2
Declaring Arrays • Can’t specify the length in a declaration int test [] = new int [100]; // Okay int test 2 [100]; // No good int test 3 [100]=new int [100]; // No good April 15, 1998 CS 102 -02 Lecture 3 -2
Some Array Definitions These create array objects: Exception ae[] = new Exception[3]; Object aao[][] = new Exception[2][3]; int[] factorial = { 1, 1, 2, 6, 24 }; String[] aas = {"array", "of", "String"}; April 15, 1998 CS 102 -02 Lecture 3 -2
An Array Example int face; int frequency[]; // initialize instance variables public void init() { frequency = new int[ 7 ]; for (int roll=1; roll <= 6000; roll++ ) { face = 1 + (int) (Math. random() * 6 ); ++frequency[ face ]; } } April 15, 1998 CS 102 -02 Lecture 3 -2
An Array of Observations • Arrays know their own length test. length == 100 – What kind of loops do we usually use with arrays? • Arrays start at index 0 – Last index == ? ? • Arrays must be indexed by int values (short, byte, or char are okay, but long is no good) April 15, 1998 CS 102 -02 Lecture 3 -2
Operating on an Array • Square brackets are an operator • Brackets have very high precedence – Same as ()’s • Example -17*student. Grades[12] * 2 April 15, 1998 CS 102 -02 Lecture 3 -2
"Can he do that? " • What’s wrong with the following code? int scores[] = {98, 76, 84, 97, 101, 78}; int counter=0; for (; counter <= scores. length; counter++) { System. out. println("Test #" + counter + ": " + scores[counter]); } April 15, 1998 CS 102 -02 Lecture 3 -2
Array Initializers • Use {}’s to set initial values – int grades[] = {87, 92, 76, 94} April 15, 1998 CS 102 -02 Lecture 3 -2
Finally, a Constant Variable • What’s wrong with the following code? // Figure 5. 6 // This Applet won’t compile import java. applet. Applet; public class Final. Test extends Applet { final int x; } April 15, 1998 CS 102 -02 Lecture 3 -2
Pass the Salt, and an Array of int • From last time: primitive types and reference types • Parameter passing: offering data to a method in the method call public void paint(Graphics graph. Object) – When you call paint, give it a Graphics object • How does Java pass data around? April 15, 1998 CS 102 -02 Lecture 3 -2
Call Me Ishmael, But Don't Call Me Late for Dinner • Call-by-value – The called method gets a copy of the actual data (i. e. , its value) – Called method can't corrupt the original data, because it's only got a copy • Call-by-reference – The called method gets a reference to the data April 15, 1998 CS 102 -02 Lecture 3 -2
Arrays & Call-by-Reference • Reference types are always passed call -by-reference – Pro: Don’t need to copy lots of data – Con: Since you can access the original object, you can change it • Arrays are a reference type – Whole arrays are passed call-by-reference April 15, 1998 CS 102 -02 Lecture 3 -2
What About Array Elements? • If an array is an array of a reference type, then individual elements are passed as call-by-______ Graphics[] graphics. Array = new Graphics[10]; • If an array is an array of a reference type, then individual elements are passed as call-by-______ int[] scores = new int[10]; April 15, 1998 CS 102 -02 Lecture 3 -2
Searching & Sorting Data • The Second Law of Thermodynamics • Sorting restores order to the world • Sorting in everyday life – Bank checks by account • Searching is easier when sorted – Reverse directories of phone numbers April 15, 1998 CS 102 -02 Lecture 3 -2
Sorting in Computers • Usually start with an array of items to be sorted – Integers – Social Security numbers – Last names and first names • Lots of data – All the SSN’s in the U. S. – All of GM’s employees April 15, 1998 CS 102 -02 Lecture 3 -2
Different Ways to Sort • Bubble sort – Compare two neighbors, swap if out of order 1 2 5 4 7 8 6 1 2 4 5 7 6 8 1 2 4 5 6 7 8 April 15, 1998 CS 102 -02 Lecture 3 -2
The Code for Bubble Sort Nested for loops public void sort() { int hold; // temporary holding area for swap // passes for ( int pass = 1; pass < a. length; pass++ ) // one pass for ( int i = 0; i < a. length - 1; i++ ) // one comparison if ( a[ i ] > a[ i + 1 ] ) { // one swap hold = a[i]; a[ i ] = a[ i + 1 ]; a[ i + 1 ] = hold; } } April 15, 1998 CS 102 -02 Lecture 3 -2
There’s More Than One Way to Sort a Cat • Other sorts – Quicksort – Heapsort – Bucket sort • Different sorts have different characteristics – Memory usage – Speed April 15, 1998 CS 102 -02 Lecture 3 -2
Searching Here, There & Everywhere • Need to find a value? Search for it! • Linear search – Is it the next one? • No, then look again • Yes, stick a fork in me, ‘cuz I’m done – Works on unsorted items • Binary search – Cut in half every time April 15, 1998 CS 102 -02 Lecture 3 -2
You’re a Detective in a Small Town • The killer called the victim • Get the LUDs • Look up the number in a reverse directory April 15, 1998 CS 102 -02 Lecture 3 -2
Arrays of Arrays • Arrays in multiple dimensions – Not just a list – A table (an array of arrays) – A cube (an array of arrays) April 15, 1998 CS 102 -02 Lecture 3 -2
An Example An array of arrays int b[][] = { {1, 2}, {3, 4, 5}} Row 0: 1 2 Row 1: 3 4 5 A 3 x 3 array int b[][]; b = new int[3][3]; April 15, 1998 CS 102 -02 Lecture 3 -2
- Slides: 25