1 / 16

A Wide Array of Possibilities

A Wide Array of Possibilities. Java’s Central Casting. CS 102-01 Lecture 1-3. 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.

atanner
Télécharger la présentation

A Wide Array of Possibilities

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Wide Array of Possibilities Java’s Central Casting CS 102-01 Lecture 1-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

  3. 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 }

  4. The Declaration of Array Array declarations int[] ai; // array of int short[][] as; // array of array of short Object[] ao, // array of Object otherAo; // array of Object

  5. Declaring Arrays • Can’t specify the length in a declaration int test [] = new int [100]; // Okay int test2 [100]; // No good int test3 [100]=new int [100]; // No good

  6. 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"};

  7. 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 ]; } }

  8. An Array of Observations • Arrays know their own length test.length == 100 • 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)

  9. Operating on an Array • Square brackets are an operator • Brackets have very high precedence • Same as ()’s • Example -17*studentGrades[12] * 2

  10. "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]); }

  11. Array Initializers • Use {}’s to set initial values • int grades[] = {87, 92, 76, 94}

  12. 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

  13. 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

  14. What About Array Elements? • If an array is an array of a reference type, then individual elements are passed as call-by-____________ Graphics[] graphicsArray = new Graphics[10]; • If an array is an array of a primitive type, then individual elements are passed as call-by-____________ int[] scores = new int[10];

  15. Arrays of Arrays • Arrays in multiple dimensions • Not just a list • A table (an array of arrays) • A cube (an array of arrays of arrays)

  16. An Example An array of arrays int b[][] = { {1,2}, {3, 4, 5}} Row 0: 1 2 Row 1: 3 4 5 A 3x3 array int b[][]; b = new int[3][3];

More Related