Chapter 8 - ArraysChapter GoalsArraysSlide 4Data TypeSlide 6Slide 7SizeMemory DiagramSlide 10Slide 11ExampleSample RunInitial ValuesLengthSlide 16ModificationsSlide 18Initializing ArraysArray SizesSlide 21Slide 22Slide 23Arrays of ObjectsSlide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Deleting ObjectsWrapper ClassesSlide 42Slide 43Finding the AverageCountingFinding a ValueMaximum/MinimumCounting Using ObjectsSearching Using ObjectsMaximum Using ObjectsMulti-Dimensional Arrays2D ArraysSlide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60QuestionsCopying ArraysSlide 63CloningSlide 65Copying ElementsCopying certain elementsCopying Certain ElementsInserting a ValueDeleting a ValueGrowing ArraysSlide 72Passing Arrays to MethodsSlide 74Slide 75Slide 76Slide 77Example: Candy in a MachineSlide 79Slide 80Slide 81Chapter 8 - Chapter 8 - ArraysArraysChapter GoalsChapter GoalsLearn arraysLearn arraysArrayLists will be left out, learn in 367 ArrayLists will be left out, learn in 367 along with auto-boxingalong with auto-boxingWrapper classesWrapper classesMulti-dimensional arraysMulti-dimensional arraysArraysArraysCommon to want to deal with collection of Common to want to deal with collection of itemsitemsKeep information about all 302 studentsKeep information about all 302 studentsInformation about all video files on computerInformation about all video files on computerAll of my grades since kindergartenAll of my grades since kindergartenBooks in a libraryBooks in a libraryInfeasible to enumerate an identifier for Infeasible to enumerate an identifier for eacheachbook1, book2, book3, …., book500book1, book2, book3, …., book500ArraysArraysArrays are a sequence of spots to Arrays are a sequence of spots to store a certain data typestore a certain data typeTwo componentsTwo componentsData TypeData TypeSizeSizeData TypeData TypeWhat type of element do we want to What type of element do we want to store in our array?store in our array?Can be a primitive or reference typeCan be a primitive or reference typeSyntax:Syntax:<datatype>[] <identifier>;<datatype>[] <identifier>;<dataype> <identifier>[];<dataype> <identifier>[];ArraysArraysProblem: Want to display differences Problem: Want to display differences in scores from averagein scores from averageWe have to keep track of We have to keep track of eacheach score scoreWe could just create one identifier for all We could just create one identifier for all 10 scores (10 scores (score1score1, , score2score2…)…)But what if we wanted to take in more But what if we wanted to take in more scores?scores?Solution: Use an Solution: Use an arrayarray of scores to of scores to store all scoresstore all scoresArraysArraysdouble[] scores; double[] scores; ORORdouble scores[];double scores[];SizeSizeTo create an array, we must use the To create an array, we must use the newnew operator operatorSyntax:Syntax:<identifier> = new <datatype>[<size>];<identifier> = new <datatype>[<size>];Examples:Examples:scores = new double[10];scores = new double[10];double[] scores = new double[10];double[] scores = new double[10];What data type is an array?What data type is an array?Memory DiagramMemory Diagramscorescoress 0 1 20 1 2 3 4 5 6 7 8 9 3 4 5 6 7 8 9ArraysArraysCan access an individual item in the Can access an individual item in the collectioncollectionUse a single identifier for the whole Use a single identifier for the whole collection (scores) and then use an collection (scores) and then use an indexed indexed expressionexpression to access an to access an array elementarray elementZero based indexing! Zero based indexing! (just like Strings)(just like Strings)Index numbered Index numbered 00 through through <size>-1<size>-1scores[0]scores[0]//1//1stst element elementscores[2]scores[2]//3//3rdrd element elementCan use expressions like Can use expressions like scores[i+1]scores[i+1]Memory DiagramMemory Diagramscorescoress 0 1 20 1 2 3 4 5 6 7 8 9 3 4 5 6 7 8 9scores[2]scores[2]ExampleExampledouble[] scores = new double[10];double[] scores = new double[10];double avg, sum = 0;double avg, sum = 0;for(int i=0; i < 10; i++){for(int i=0; i < 10; i++){System.out.print(“Enter Score”);System.out.print(“Enter Score”);scores[i] = in.nextDouble();scores[i] = in.nextDouble();sum += scores[i];sum += scores[i];}}avg = sum/10;avg = sum/10;for(int i=0; i < 10; i++){for(int i=0; i < 10; i++){System.out.print("Score " + i + " ");System.out.print("Score " + i + " ");System.out.print(score[i] + " ");System.out.print(score[i] + " ");System.out.println(score[i] – avg);System.out.println(score[i] – avg);}}Sample RunSample RunEnter score: 22Enter score: 22Enter score: 24Enter score: 24Enter score: 90Enter score: 90Enter score: 88Enter score: 88Enter score: 75Enter score: 75Enter score: 95Enter score: 95Enter score: 65Enter score: 65Enter score: 80Enter score: 80Enter score: 92Enter score: 92Enter score: 69Enter score: 69Score 0 22.0 -48.0Score 0 22.0 -48.0Score 1 24.0 -46.0Score 1 24.0 -46.0Score 2 90.0 20.0Score 2 90.0 20.0Score 3 88.0 18.0Score 3 88.0 18.0Score 4 75.0 5.0Score 4 75.0 5.0Score 5 95.0 25.0Score 5 95.0 25.0Score 6 65.0 -5.0Score 6 65.0 -5.0Score 7 80.0 10.0Score 7 80.0 10.0Score 8 92.0 22.0Score 8 92.0 22.0Score 9 69.0 -1.0Score 9 69.0 -1.0Initial ValuesInitial ValuesWhen array is created, all values are When array is created, all values are initialized depending on array type: initialized depending on array type: Numbers: Numbers: 00 Boolean: Boolean: falsefalse Object References: Object References: nullnull Means compiler does not force Means compiler does not force initialization like with primitivesinitialization like with primitivesLengthLengthWe assumed size of array was 10…what if We assumed size of array was 10…what if we aren’t sure? Or want to change size?we aren’t sure? Or want to change size?Every array has a Every array has a public constantpublic constant data data member named member named length (no length (no parenthesis)parenthesis)for(int i=0; i < for(int i=0; i < scores.lengthscores.length; i++){; i++){System.out.print("Score "+i+" ");System.out.print("Score "+i+" ");System.out.print(score[i]+" ");System.out.print(score[i]+"
View Full Document