Unformatted text preview:

Chapter 6Introduction to ArraysCreating and Accessing ArraysSlide 4Slide 5Slide 6Declaring and Creating an ArrayReferring to Arrays and Array ElementsUsing the score Array in a ProgramThree Ways to Use Square Brackets [] with an Array NameThe length Instance VariablePitfall: Array Index Out of BoundsSlide 13Initializing ArraysSlide 15Pitfall: An Array of Characters Is Not a StringSlide 17Slide 18Arrays and ReferencesArrays are ObjectsArrays Are ObjectsPitfall: Arrays with a Class Base TypeSlide 23Array ParametersSlide 25Slide 26Slide 27Slide 28Slide 29Slide 30Pitfall: Use of = and == with ArraysSlide 32Slide 33Slide 34Slide 35Slide 36Methods That Return an ArraySlide 38Partially Filled ArraysSlide 40Slide 41Privacy Leaks with Array Instance VariablesSlide 43Slide 44Sorting an ArraySelection Sort (Part 1 of 2)Selection Sort (Part 2 of 2)SelectionSort Class (Part 1 of 5)SelectionSort Class (Part 2 of 5)SelectionSort Class (Part 3 of 5)SelectionSort Class (Part 4 of 5)SelectionSort Class (Part 5 of 5)Enumerated TypesEnumerated Types ExampleEnumerated Types UsageSlide 56Slide 57An Enumerated TypeSome Methods Included with Every Enumerated Type (Part 1 of 3)Some Methods Included with Every Enumerated Type (Part 2 of 3)Some Methods Included with Every Enumerated Type (Part 3 of 3)The values MethodThe Method values (Part 1 of 2)The Method values (Part 2 of 2)Programming Tip: Enumerated Types in switch StatementsEnumerated Type in a switch Statement (Part 1 of 3)Enumerated Type in a switch Statement (Part 2 of 3)Enumerated Type in a switch Statement (Part 3 of 3)The following programs show the use of Method values and for each loopMultidimensional ArraysOne dimensional ArrayTwo dimensional ArraySlide 73Ragged 2D ArraySlide 75Slide 76Slide 77Two-Dimensional Array as an Array of Arrays (Part 1 of 2)Two-Dimensional Array as an Array of Arrays (Part 2 of 2)Using the length Instance VariableSlide 81Ragged ArraysSlide 83Slide 84Multidimensional Array Parameters and Returned ValuesSlide 86A Grade Book ClassSlide 88The Two-Dimensional Array gradeExample of Two-Dimensional ArraySlide 91Slide 92Slide 93Slide 94Slide 95Slide 96Slide 97Slide 98Slide 99Chapter 6ArraysCopyright © 2008 Pearson Addison-Wesley. All rights reservedIntroduction to Arrays•An array is a data structure used to process a collection of data that is all of the same type–An array behaves like a numbered list of variables with a uniform naming mechanism–It has a part that does not change: the name of the array–It has a part that can change: an integer in square brackets–For example, given five scores:score[0], score[1], score[2], score[3], score[4]6-2Copyright © 2008 Pearson Addison-Wesley. All rights reservedCreating and Accessing Arrays•An array that behaves like this collection of variables, all of type double, can be created using one statement as follows:double[] score = new double[5];•Or using two statements:double[] score;score = new double[5];–The first statement declares the variable score to be of the array type double[]–The second statement creates an array with five numbered variables of type double and makes the variable score a name for the array6-3Copyright © 2008 Pearson Addison-Wesley. All rights reservedCreating and Accessing Arrays•The individual variables that together make up the array are called indexed variables–They can also be called subscripted variables or elements of the array–The number in square brackets is called an index or subscript–In Java, indices must be numbered starting with 0, and nothing elsescore[0], score[1], score[2], score[3], score[4]6-4Copyright © 2008 Pearson Addison-Wesley. All rights reservedCreating and Accessing Arrays•The number of indexed variables in an array is called the length or size of the array•When an array is created, the length of the array is given in square brackets after the array type •The indexed variables are then numbered starting with 0, and ending with the integer that is one less than the length of the arrayscore[0], score[1], score[2], score[3], score[4]6-5Copyright © 2008 Pearson Addison-Wesley. All rights reservedCreating and Accessing Arraysdouble[] score = new double[5];•A variable may be used in place of the integer (i.e., in place of the integer 5 above) –The value of this variable can then be read from the keyboard–This enables the size of the array to be determined when the program is rundouble[] score = new double[count];•An array can have indexed variables of any type, including any class type•All of the indexed variables in a single array must be of the same type, called the base type of the array6-6Copyright © 2008 Pearson Addison-Wesley. All rights reservedDeclaring and Creating an Array•An array is declared and created in almost the same way that objects are declared and created:BaseType[] ArrayName = new BaseType[size];–The size may be given as an expression that evaluates to a nonnegative integer, for example, an int variablechar[] line = new char[80];double[] reading = new double[count];Person[] specimen = new Person[100];6-7Copyright © 2008 Pearson Addison-Wesley. All rights reservedReferring to Arrays and Array Elements•Each array element can be used just like any other single variable by referring to it using an indexed expression: score[0]•The array itself (i.e., the entire collection of indexed variables) can be referred to using the array name (without any square brackets): score•An array index can be computed when a program is run–It may be represented by a variable: score[index]–It may be represented by an expression that evaluates to a suitable integer: score[next + 1]Note: Demo ArrayOfScores6-8Copyright © 2008 Pearson Addison-Wesley. All rights reservedUsing the score Array in a Program•The for loop is ideally suited for performing array manipulations:for (index = 0; index < 5; index++) System.out.println(score[index] + " differs from max by " + (max-score[index]) );6-9Copyright © 2008 Pearson Addison-Wesley. All rights reservedThree Ways to Use Square Brackets [] with an Array Name•Square brackets can be used to create a type name:double[] score;•Square brackets can be used with an integer value as part of the special syntax Java uses to create a new array:score = new double[5];•Square brackets can be used to name an indexed variable of an array:max = score[0];6-10Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe


View Full Document

SJU CSC 3405 - Lecture notes

Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?