120 likes | 148 Views
Introduction to Arrays. This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. What’s an Array?. Array: a data structure used to process a collection of data that is all of the same type Three components:
E N D
Introduction to Arrays This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing
What’s an Array? • Array: a data structure used to process a collection of data that is all of the same type • Three components: • Array name: the name of this collection of data • Example: score • Array elements: each data item with a 0-based index • Example: score[0], score[1], … score[4] • Array length: the number of data items • Example: 5 An integer array score [0] 15 [1] 40 [2] -3 [3] 99 [4] -407 CSS161: Fundamentals of Computing
Creating Arrays • Syntax 1 BaseType[] ArrayName = new BaseType[size]; where • BaseType : the single data type of all the array elements • ArrayName : the name of the array created • size : the number of the array elements • Syntax 2 BaseType[] ArrayName; // declare an array of BaseType ArrayName = new BaseType[size]; // instantiate an array object with size elements. • Syntax 1 is shorthand for Syntax 1. • Example: double score = new double[5]; int size = 10; char message = new char[size]; A double array score [0] 15.05 [1] 40.11 [2] -3.14 [3] Can be specified with an int variable 99.99 [4] -407.0 CSS161: Fundamentals of Computing
Referring to Arrays and Array Elements • Referring to an array double[] score = new double[5]; double[] copy = score; int[] illegalCopy = score; • Referring to an array element // an indexed element can receive an appropriate type of value score[0] = 1.41421356; score[1] = 1.73205080; // an indexed element can retrieve its data as a variable double sum = score[0] + score[1]; score[2] = score[0] + score[1]; • Computing an array index // an index can be computed at run time. int next = 1; score[index + 1] = score[index] // i.e. score[2] = score[1] A double array [0] [1] [2] [3] [4] CSS161: Fundamentals of Computing
A double array [0] Nothing yet 2.71828.. [1] [2] [3] [4] Three Ways to Use Square Brackets[] with an Array Name • Declare an array reference double[] score; • Create an array score = new double[5]; • Refer to an array element score[0] = 2.7182845904; CSS161: Fundamentals of Computing
for loop to perform array manipulations • The for loop is ideally suited for performing array manipulations: for(index = 0; index < 5; index++) System.out.println( “score[” + idnex + “] =” score[index] ); CSS161: Fundamentals of Computing
The length Instance Variable • An array: is considered to be an object that has some instance variables. • length: an instance variable of each array to show the number of its array elements • Example: double[] score = new double[5]; System.out.println( “score’s number of elements = ” + score.length); Score.length = 10; // Illegal, can’t resize CSS161: Fundamentals of Computing
Example - Display 6.2 import java.util.Scanner; publicclass ArrayOfScores2 { /** Reads in 5 scores and shows how much each score differs from the highest score. */ publicstaticvoid main(String[] args) { Scanner keyboard = new Scanner(System.in); double[] score = newdouble[5]; int index; double max; System.out.println("Enter " + score.length + " scores:"); score[0] = keyboard.nextDouble( ); max = score[0]; for (index = 1; index < score.length; index++) { score[index] = keyboard.nextDouble( ); if (score[index] > max) max = score[index]; //max is the largest of the values score[0],..., score[index]. } System.out.println("The highest score is " + max); System.out.println("The scores are:"); for (index = 0; index < score.length; index++) System.out.println(score[index] + " differs from max by “ + (max - score[index])); } } Array creation Storing a value to an array element length instance variable Retrieving a value from an array element CSS161: Fundamentals of Computing
Pitfall: Array Index Out of Bounds • In the previous example, what if we wrote: for (index = 0; index < 6; index++) System.out.println(score[index] + " differs from max by “ + (max - score[index])); • The Java compiler does not complain, but • You get a runtime error. • The range of array index if you create an array like: BaseType[] ArrayName = new BaseType[size]; the range is: 0 through to size - 1. • Array index out of bounds • It is an run-time exception when your program has accessed some value out of an array range: CSS161: Fundamentals of Computing
Initializing Arrays • Upon a creation with { } • An array of three integer elements int[] age = {2, 12, 1}; Is equivalent to: int[] age = new int[3]; age[0] = 2; age[1] = 12; age[2] = 1; • An array of 5 char elements char[] message = {'A', 'B', 'C’, ’D’, ’E'}; Is equivalent to: char[] message = new char[5]; message[0] = 'A'; message[1] = 'B'; message[2] = 'C’; message[3] = ’D’; message[4] = ’E'; • Using a for loop double[] reading = new double[100]; for (int index = 0; index < reading.length; index++) reading[index] = 42.0; • Depending on default values • In general, elements are initialized to 0, 0.0, or false. CSS161: Fundamentals of Computing
Pitfall: An Array of Characters is Not a String • An array of characters is a list of char elements char[] x = {'A', 'B', 'C'}; // x[0], x[1], and x[2] is accessible. • A string is an instance of the String class String y = "ABC"; // y[0] is illegal. Use y.charAt(0). • Thus, they are not interchangeably used. char[] x = {'A', 'B', 'C'}; String y = "ABC"; if ( x == y ) { // This boolean expression is illegal. System.out.println( ”They are identical." ) } if ( y.equals( x ) ) { // This boolean expression is illegal, too. System.out.println( ”They are identical." ) } CSS161: Fundamentals of Computing
Self-Test Exercises • Work on textbook p341 and p342’s exercises 1 ~ 6. CSS161: Fundamentals of Computing