Download presentation
Presentation is loading. Please wait.
1
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Arrays Chapter 6
2
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 2 Each variable only holds one item if > 1 item wanted, need an array array that holds a word arrays hold elements all of the same type char[ ] word = new char[4]; holds 4 elements of type char Arrays 0132 word
3
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 3 char[ ] word = new char[4]; two parts to an array: 1. index -- integer element – type inside array 'h''e' 0132 'h' 0132 word[1] = 'e'; 'h''e''o''r' 0132 'h''e''o' 0132 word[3] = 'o'; word[2] = 'r'; word[0] = 'h'; two parts to an array: 1. index -- integer two parts to an array: 1. element – any type inside
4
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 4 Can use variables for index OR elements int i=3; char newc = 'd'; word[i] = newc; can find length word.length // is 4 largest index is always length – 1 word[4] is RUN time error Array manipulation 'h''e''o''r' 0132 'h''e''d''r' 0132
5
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 5 arrays and new char[ ] word; creates word that is of type char array that points to nothing word = new char[4]; creates array of 4 elements initialized to \u0000 (Java always initializes instance variables to 0)
6
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 6 Myarray example public class Myarray { private Circle[ ] circles; private double[ ] area; // other stuff in the class }
7
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 7 Myarray gets elements allocated Create an object circles = new Circle[4]; area = new double[4];
8
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 8 createcircles( ) circles[0] = new Circle();
9
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 9 array creation summary char[ ] word; creates a space named word that contains null word = new char [4]; allocates 4 chars, initialized, word points to them classes: Circle[ ] mycircles; same as word mycircles = new Circle[4]; allocates 4 spaces that contain null mycircles[0] = new Circle( ); creates an actual circle new [ ] for arrays, NOT ( )
10
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 10 array creation summary use new to create an array: –char[ ] word = new char [4]; –Circle[ ] circles = new Circle[4]; if the type of element is an object, each item must be new-ed –word[1] = ‘r’; –circles[1] = new Circle( );
11
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 11 Repetition in arrays arrays often do the same thing (e.g., for each Circle in array, create a Circle) for (int i=0; i<circles.length; i++) circles[i] = new Circle( ); memorize this line
12
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 12 More on Loops Java 5 for loop/for each/expanded for for (Circle c : circles)c.changeColor(“red”); No explicit i; cannot reference individual elements (e.g., a[i] = d; won't work) Read ‘for (’ as “for each” and ‘:’ as “in the array” Type of element name of array variable c is name for each element in loop, NOT each index
13
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 13 Practice 1.Write code to declare a 4 character word array, then write a loop to initialize chars in word to be 'A' 2.Write code to declare a 26 character array, then write a loop to initialize chars in word to be ABCDEFGHIJKLMNOPQRSTUVWXYZ (do this in a loop). Hint: use a separate variable for the element value (start with 'A') ++ and += works for chars 3.Declare an int array 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)
14
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 14 Array Specifics: passing as a parameter Passing arrays as parameters int[ ] grades = new grades[20]; int sum = sumgrades(grades); double average = 0.0; if (grades.length > 0) average = sum * 1.0 / grades.length; int index; index = findmatches(grades[0], grades, 1, grades.length); if (index == -1) System.out.println("Match not found"); To call a method with an array as a parameter, use just the name of the array. NO [ ] To pass an element of an array, access the element as you always would
15
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 15 Write sumgrades Call is: int sum = sumgrades(grades);
16
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 16 Arrays as Parameters public int sumgrades(int[ ] myarray) { int sum = 0; for (int element : myarray) sum += element; return sum; } or public int sumgrades(int[ ] myarray) { int sum = 0; for (int i=0;i<myarray.length;i++) sum = sum + myarray[i]; return sum; } Declare array parameters using []s just like when you declare them as variables
17
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 17 Write findmatches Call is: index = findmatches(grades[0], grades, 1, grades.length);
18
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 18 Searching Arrays public int findmatches(int item, int[ ] anarray, int start, int finish) { for (int i=start;i<finish;i++) if (item == anarray[i]) return i; return -1; // if done with loop, not found }
19
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 19 Initializing and Copying arrays Can initialize arrays –int[ ] myarrray = {3, 5, 7, 9}; Copying arrays –int yourarray[ ] = new int[myarray.length]; –yourarray = myarray; –copies reference, not values. –must copy individual elements: for (int i=0;i<myarray.length;i++) yourarray[i] = myarray[i]; 3579 myarray
20
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 20 Two-dimensional Arrays int students = 5; int nbrgrades = 3; int[ ][ ] grades = new int[students][nbrgrades]; [0] [1] [2] [4] [3] [1][0][2] Student Number Grades
21
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 21 Two-dimensional Arrays int students = 5; int nbrgrades = 3; int[ ][ ] grades = new int[students][nbrgrades]; for (int i=0;i<grades.length;i++) { System.out.print("Student number " + i + " "); for (int j=0;j<grades[i].length;j++) System.out.print(grades[i][j] + " "); System.out.println(); }
22
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 22 Write a method that returns the average of all grades int students = 5; int nbrgrades = 3; int grades = new int[students][nbrgrades]; header is: public double average(int[ ] [ ] myarray)
23
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 23 More array practice Write a method that finds the largest element in an array of integers Write a method that will print all of the scores greater than the average
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.