Click to edit Master title style Click to

  • Slides: 19
Download presentation
Click to edit Master title style • • • Click to edit Master text

Click to edit Master title style • • • Click to edit Master text styles Second level Arrays Third level Fourth level Fifth level 1

Click to edit. Arrays Master title style • Each Click to variable edit Master

Click to edit. Arrays Master title style • Each Click to variable edit Master only holds text styles one item • if. Second > 1 item level wanted, need an array • array Third level that holds a word • arrays Fourth hold levelelements all of the same type • Fifth] word char[ level = new char[4]; • holds 4 elements of type char word 0 1 2 3 2

Click to edit Master title style char[ ] word = new char[4]; element inside

Click to edit Master title style char[ ] word = new char[4]; element inside two parts –totype an array: • Click to edit Master text styles array 1. element – any type inside • Second level word[3] = 'o'; word[0] = 'h'; • Third level 'h' 'e' 'h' • Fourth level 0 1 2 3 • Fifth level two parts to an array: 2. index -- integer word[1] = 'e'; 'h' 'e' 0 1 'o' 2 3 word[2] = 'r'; 2 3 'h' 'e' 'r' 'o' 0 1 2 3 3

Click. Array to editmanipulation Master title style 'h' to 'e' edit 'r' 'o' •

Click. Array to editmanipulation Master title style 'h' to 'e' edit 'r' 'o' • Click Master text styles 0 1 2 3 • • Second level for index OR elements Can use variables int i=3; char newc = 'd'; • Third level word[i] = newc; • Fourth level 'h' 'e' 'r' 'd' • Fifth level 0 1 2 3 • can find length word. length // is 4 • largest index is always length – 1 • word[4] is RUN time error 4

Click toarrays edit Master and new title style • char[ Click] to word; edit

Click toarrays edit Master and new title style • char[ Click] to word; edit Master text styles • creates Second word level that is of type char array that to nothing • points Third level word = new char[4]; • Fourth level • • creates array of 4 elements initialized to Fifth level u 0000 (Java always initializes instance variables to 0) 5

Click. Repetition to edit Master in arrays title style • arrays Click tooften edit

Click. Repetition to edit Master in arrays title style • arrays Click tooften edit Master do the same text styles thing • Second (e. g. , for each levelchar in array, assign ‘Z’ to memorize this line • char) Third level • Fourth level for (int level i=0; i<word. length; i++) • Fifth word[i] = ‘Z’; 6

Click to. More edit Master on Loops title style • Java Click 5 tofor

Click to. More edit Master on Loops title style • Java Click 5 tofor editloop/for Mastereach/expanded text styles for Read ‘for (’ as “for each” of element • Type Second level and ‘: ’ as “in the array” • Third level name of array variable for (char c : word) • Fourth level System. out. println(c); // NOT c[i] • name Fifthfor level c is each element in loop, NOT each index NOTE: you cannot change values in a for each No explicit i; cannot reference individual elements (e. g. , a[i] = d; won't work)7

Practice Click to edit Master title style 1. Write code to declare a 4

Practice Click to edit Master title style 1. Write code to declare a 4 character word array, then write a loop to initialize chars in word to be • Click 'A' to edit Master text styles 2. Write code to declare a 26 character array, then • Second level write a loop to initialize chars in word to be ABCDEFGHIJKLMNOPQRSTUVWXYZ (do this • Third level in a loop). Hint: use a separate variable for the • Fourth elementlevel value (start with 'A') ++ and += works for chars • Fifth level 3. Declare an int array myarray with 1000 integers and write a loop to put the value of the index into the element (e. g. , intarray[3] should have the value 3). 4. Write a loop to print all odd elements in myarray. Use a for each loop. 8

Array Specifics: passing as a Click to edit Master title style parameter To call

Array Specifics: passing as a Click to edit Master title style parameter To call a method with an • Passing as parameters Click to arrays edit Master text styles array as a parameter, use just the name of the array. NO [ ] int[ ] grades = new grades[20]; • Second level int sum = sumgrades(grades); // calculate sum • double Third level= 0. 0; average if • (grades. length Fourth level> 0) average = sum * 1. 0 / grades. length; // calc ave • Fifth level int index; index = findmatches(grades[0], grades, 1, grades. length); if (index == -1) To pass an element of an array, access System. out. println("Match not found"); the element as you always would 9

Click to Write editsumgrades Master title style • Call Clickis: to edit Master text

Click to Write editsumgrades Master title style • Call Clickis: to edit Master text styles int sum = sumgrades(grades); • Second level • Third level • Fourth level • Fifth level 10

Initializing Click to edit and Master Copying titlearrays style • • Can initialize arrays

Initializing Click to edit and Master Copying titlearrays style • • Can initialize arrays Click edit Master text styles – int[ ]tomyarrray = {3, 5, 7, 9}; myarray Second level 3 5 7 9 Third level Copying arrays Fourth level – int yourarray[ ] = new int[myarray. length]; Fifth level = myarray; – yourarray – copies reference, not values. – must copy individual elements: for (int i=0; i<myarray. length; i++) yourarray[i] = myarray[i]; 11

Click Arrays to editas. Master Parameters title style public int sumgrades(int[ ] myarray) {

Click Arrays to editas. Master Parameters title style public int sumgrades(int[ ] myarray) { • int Click to edit Master text styles Declare array parameters sum = 0; using []s just like when you (int element : myarray) • for Second level declare them as variables sum += element; • return Thirdsum; level } • Fourth level or • public Fifth int level sumgrades(int[ ] myarray) { int sum = 0; for (int i=0; i<myarray. length; i++) sum = sum + myarray[i]; return sum; } 12

Click Write to editfindmatches Master title style • Call Clickis: to edit Master text

Click Write to editfindmatches Master title style • Call Clickis: to edit Master text styles index = findmatches(grades[0], grades, 1, grades. length); • Second level When passing an • Array Third levelin call elements entire array, use the use [ ]s • Fourth level name of the array, no [ ]s • • Method Header is: Fifth level int findmatches(int item, int[ ] anarray, int start, int finish) Array elements have no [ ]s; use them as though they were not in an array Array parameters use []s in the call, just like declaring them as variables 13

Click Searching to edit Master Arrays title style public item, styles int[ ] anarray,

Click Searching to edit Master Arrays title style public item, styles int[ ] anarray, • Clickinttofindmatches(int edit Master text int start, int finish) • { • • • Second level Third level for (int i=start; i<finish; i++) Fourth level if (item == anarray[i]) Fifth level return i; return -1; // if done with loop, not found } 14

Click Two-dimensional to edit Master title Arrays style • int Click students to edit

Click Two-dimensional to edit Master title Arrays style • int Click students to edit = Master 5; int nbrgrades text styles = 3; • int[ ][ ] grades Second level= new int[students][nbrgrades]; • Third level[0] • Fourth level [1] • Student Fifth. Number level[2] [3] [4] [0] [1] Grades [2] 15

Click Two-dimensional to edit Master title Arrays style • • int students 5; int

Click Two-dimensional to edit Master title Arrays style • • int students 5; int nbrgrades = 3; Click to edit= Master text styles • int[ ][ ] grades = new int[students][nbrgrades]; • Second level for (int i=0; i<grades. length; i++) • { Third level • System. out. print("Student Fourth level number " + i + " "); for (intlevel j=0; j<grades[i]. length; j++) • Fifth System. out. print(grades[i][j] + " "); System. out. println(); } 16

Write a method that returns the Click to edit Master title style average of

Write a method that returns the Click to edit Master title style average of all grades • int Click students to edit = Master 5; int nbrgrades text styles = 3; • int Second grades level = new int[students][nbrgrades]; • Third level • header Fourth level is: • Fifth double public level average(int[ ] myarray) 17

Method returns the average grades Clickthat to edit Master titleofstyle • int students =

Method returns the average grades Clickthat to edit Master titleofstyle • int students = 5; int nbrgrades = 3; Click to edit= new Master text styles • • int[ ][ ] grades int[students][nbrgrades]; • public Second level double average(int[ ] myarray) {int sum = level 0; • Third for (int i=0; i<grades. length; i++) • { Fourth level for (intlevel j=0; j<grades[i]. length; j++) { • Fifth sum += grades[i][j]; } } if ((grades[i]. length * grades. length) == 0) return 0; else return sum / grades[i]. length * grades. length; } 18

Click. More to edit array Master practice title style • • • Write to

Click. More to edit array Master practice title style • • • Write to Click a method edit Master that text findsstyles the largest element in an array of integers Second level Write a method that will print all of the Third level scores greater than the average Fourth level Fifth level 19