230 likes | 321 Views
Arrays. Chapter 6. 0. 1. 2. 3. Arrays. 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. word. 'h'. 0. 1. 2. 3. 'h'. 'e'. 0. 1. 2.
E N D
Arrays Chapter 6
0 1 2 3 Arrays • 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 word
'h' 0 1 2 3 'h' 'e' 0 1 2 3 'h' 'h' 'e' 'e' 'r' 'o' 'o' 0 0 1 1 2 2 3 3 char[ ] word = new char[4]; two parts to an array: 1. index -- integer element – type inside array two parts to an array: 1. element – any type inside word[3] = 'o'; word[0] = 'h'; two parts to an array: 1. index -- integer word[1] = 'e'; word[2] = 'r';
'h' 'h' 'e' 'e' 'r' 'r' 'o' 'd' 0 0 1 1 2 2 3 3 Array manipulation • 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
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)
Myarray example public class Myarray { private Circle[ ] circles; private double[ ] area; // other stuff in the class }
Myarray gets elements allocated • Create an object circles = new Circle[4]; area = new double[4];
createcircles( ) • createcircles() circles[0] = new Circle();
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 ( )
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( );
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
More on Loops • Java 5 for loop/for each/expanded for Read ‘for (’ as “for each” and ‘:’ as “in the array” Type of element name of array variable for (Circle c : circles) c.changeColor(“red”); c is name for each element in loop, NOT each index No explicit i; cannot reference individual elements (e.g., a[i] = d; won't work)
Practice • Write code to declare a 4 character word array, then write a loop to initialize chars in word to be 'A' • 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 • 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)
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
Write sumgrades • Call is: int sum = sumgrades(grades);
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
Write findmatches • Call is: index = findmatches(grades[0], grades, 1, grades.length);
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 }
myarray 3 5 7 9 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];
Two-dimensional Arrays • int students = 5; int nbrgrades = 3; • int[ ][ ] grades = new int[students][nbrgrades]; [0] [1] Student Number [2] [3] [4] [0] [1] [2] Grades
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(); }
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)
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