UW-Madison CS 302 - Chapter 5 - The Socialist Compass

Unformatted text preview:

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 GoalsLearn arraysLearn arraysArrayLists will be left out, learn in 367 ArrayLists will be left out, learn in 367 along with auto-boxingalong with auto-boxingWrapper classesWrapper classesMulti-dimensional arraysMulti-dimensional arraysArraysArraysCommon to want to deal with collection of Common to want to deal with collection of itemsitemsKeep information about all 302 studentsKeep information about all 302 studentsInformation about all video files on computerInformation about all video files on computerAll of my grades since kindergartenAll of my grades since kindergartenBooks in a libraryBooks in a libraryInfeasible to enumerate an identifier for Infeasible to enumerate an identifier for eacheachbook1, book2, book3, …., book500book1, book2, book3, …., book500ArraysArraysArrays are a sequence of spots to Arrays are a sequence of spots to store a certain data typestore a certain data typeTwo componentsTwo componentsData TypeData TypeSizeSizeData TypeData TypeWhat 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>[];ArraysArraysProblem: Want to display differences Problem: Want to display differences in scores from averagein scores from averageWe have to keep track of We have to keep track of eacheach score scoreWe 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[];SizeSizeTo 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 9ArraysArraysCan access an individual item in the Can access an individual item in the collectioncollectionUse 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 elementZero based indexing! Zero based indexing! (just like Strings)(just like Strings)Index numbered Index numbered 00 through through <size>-1<size>-1scores[0]scores[0]//1//1stst element elementscores[2]scores[2]//3//3rdrd element elementCan 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 ValuesWhen 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 primitivesLengthLengthWe 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

UW-Madison CS 302 - Chapter 5 - The Socialist Compass

Download Chapter 5 - The Socialist Compass
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 Chapter 5 - The Socialist Compass 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 Chapter 5 - The Socialist Compass 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?