Unformatted text preview:

Chapter 6 ArraysMotivationsObjectivesIntroducing ArraysDeclaring Array VariablesCreating ArraysDeclaring and Creating in One StepThe Length of an ArrayDefault ValuesIndexed VariablesSlide 11Using Indexed VariablesArray InitializersDeclaring, creating, initializing Using the Shorthand NotationCAUTIONTrace Program with ArraysSlide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Processing ArraysInitializing ArraysPrinting ArraysSumming all elementsFinding the largest elementFinding the smallest index of the largest elementShifting elementsEnhanced for Loop (for-each loop)Example: Analyzing Array ElementsTestArray.javaSlide 42Slide 43Problem: Assigning GradesAssignGrade.javaSlide 46Copying ArraysSlide 48The arraycopy UtilityPassing Arrays to MethodsAnonymous ArrayPass By ValueSimple ExampleCall StackHeapPassing Arrays as ArgumentsTestPassArray.javaSlide 58Returning an Array from a MethodTrace the reverse MethodTrace the reverse Method, cont.Slide 62Slide 63Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Slide 78Slide 79Slide 80Slide 81Problem: Counting Occurrence of Each LetterCountLettersInArray.javaSlide 84Slide 85Searching ArraysLinear SearchLinear Search AnimationFrom Idea to SolutionLinearSearch.javaLinearSearchTest.javaBinary SearchBinary Search, cont.Slide 94Slide 95Slide 96Slide 97From Idea to SolutonBinarySearch.javaBinarySearchTest.javaThe Arrays.binarySearch MethodBinarySearchArrayTest.javaSorting ArraysSelection SortSlide 105Slide 106ExpandSlide 108Slide 109Wrap it in a MethodSelectionSort.javaSelectionSortTest.javaInsertion SortSlide 114How to Insert?Slide 116InsertionSort.javaInsertionSortTest.javaThe Arrays.sort MethodTwo-dimensional ArraysDeclare/Create Two-dimensional ArraysDeclaring Variables of Two-dimensional Arrays and Creating Two-dimensional ArraysTwo-dimensional Array IllustrationDeclaring, Creating, and Initializing Using Shorthand NotationsLengths of Two-dimensional ArraysLengths of Two-dimensional Arrays, cont.Ragged ArraysRagged Arrays, cont.Problem: Grading Multiple-Choice TestGradeExam.javaSlide 131Problem: Finding Two Points Nearest to Each OtherFindNearestPoints.javaSlide 134Case Study: SudokuSlide 136Multidimensional ArraysProblem: Calculating Total ScoresRefine the tableReorganize the tableDeclare Two ArraysLiang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126711Chapter 6 ArraysLiang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126712MotivationsOften you will have to store a large number of values during the execution of a program. Suppose, for instance, that you need to read one hundred numbers, compute their average, and find out how many numbers are above the average. Your program first reads the numbers and computes their average, and then compares each number with the average to determine whether it is above the average. The numbers must all be stored in variables in order to accomplish this task. You have to declare one hundred variables and repeatedly write almost identical code one hundred times. From the standpoint of practicality, it is impossible to write a program this way. So, how do you solve this problem?Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126713ObjectivesTo describe why arrays are necessary in programming (§6.1).To learn the steps involved in using arrays: declaring array reference variables and creating arrays (§§6.2.1-6.2.2).To initialize the values in an array (§6.2.3).To access array elements using indexed variables (§6.2.4).To simplify programming using the JDK 1.5 foreach loops (§6.2.5).To declare, create, and initialize an array using an array initializer (§6.2.6).To copy contents from one array to another (§6.3).To develop and invoke methods with array arguments and return value (§6.4-6.5).To declare a method with variable-length argument list (§6.6).To search elements using the linear (§6.7.1) or binary (§6.7.2) search algorithm.To sort an array using the selection sort (§6.8.1)To sort an array using the insertion sort algorithm (§6.8.2).To use the methods in the Arrays class (§6.9).To declare and create two-dimensional arrays to solve interesting problems such as Sudoku (§6.10).To declare and create multidimensional arrays (§6.11).Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126714Introducing ArraysArray is a data structure that represents a collection of the same types of data. 5.6 4.5 3.3 13.2 4 34.33 34 45.45 99.993 11123 double[] myList = new double[10]; myList reference myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] Element value Array reference variable Array element at index 5Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126715Declaring Array Variablesdatatype[] arrayRefVar;Example: double[] myList;datatype arrayRefVar[]; // This style is allowed, but not preferredExample: double myList[];Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126716Creating ArraysarrayRefVar = new datatype[arraySize];Example:myList = new double[10];myList[0] references the first element in the array.myList[9] references the last element in the array.Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126717Declaring and Creating in One Stepdatatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];datatype arrayRefVar[] = new datatype[arraySize];double myList[] = new double[10];Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126718The Length of an ArrayOnce an array is created, its size is fixed. It cannot be changed. You can find its size usingarrayRefVar.lengthFor example,myList.length returns 10Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 01360126719Default ValuesWhen an array is created, its elements are assigned the default value


View Full Document

UNCP CSC 1750 - Arrays

Download Arrays
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 Arrays 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 Arrays 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?