declaring array variables chapter 6 arrays
play

Declaring Array Variables Chapter 6 Arrays datatype[] arrayRefVar; - PDF document

Declaring Array Variables Chapter 6 Arrays datatype[] arrayRefVar; Preferred! Example: double[] myList; datatype arrayRefVar[]; Example: double myList[]; 1 4 Motivations Creating Arrays Suppose we want to write a program that


  1. Declaring Array Variables Chapter 6 Arrays  datatype[] arrayRefVar; Preferred! Example: double[] myList;  datatype arrayRefVar[]; Example: double myList[]; 1 4 Motivations Creating Arrays  Suppose we want to write a program that arrayRefVar = new datatype[arraySize]; computes the average midterm score of all the students and find out how many scored above the Example: average myList = new double[10];  How many variables do we need for storing the scores? (30 students!)  Solution: Arrays 2 5 Arrays Declaring and Creating an Array in One Step  Array is a data structure that represents a Datatype[] arrayRefVar; collection of the same types of data. arrayRefVar = new datatype[arraySize];  How to use arrays?  Declaring array variables datatype[] arrayRefVar =  Creating arrays  Initializing arrays new datatype[arraySize];  Accessing and using array elements  Traversing arrays using for loops Example: double[] myList = new double[10]; 6

  2. Array Initializers - Declaring, creating, Default Values initializing an array using one statement  When an array is created, its elements are assigned the default value: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following  0 for the numeric primitive data types statements:  (char)0 for char types double[] myList = new double[4];  false for boolean types myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; Note: this shorthand syntax must be in one statement 7 10 Assigning Values to Array Elements The Length of an Array arrayRefVar[index] = expression; Once an array is created, its size is fixed. It cannot be changed. You can find its size using Example: myList[0] = 5.6; arrayRefVar.length myList[1] = 4.5; For example: … myList[9] = 11123; int[] numbers = new int[10]; int len = numbers.length; // 10 8 11 Inside Arrays Accessing Array Elements  The array elements are accessed through the double[] myList = new double[10]; index. myList reference 5.6  Each element in the array is represented using: myList[0] myList[1] 4.5 arrayRefVar[index] 3.3 Array reference myList[2] variable 13.2 myList[3]  The array indices starts from 0 to array.length-1 4 myList[4]  Example: Array element at myList[5] 34.33 Element value index 5 34 myList[6] 45.45 myList[7] double[] myList = new double[10]; 99.993 myList[8] myList[0] = 5.6; 11123 myList[9] myList[1] = 4.5; myList[10] = 3.33; // ArrayIndexOutOfBoundsException 9 12

  3. Using Array Elements Example  Use elements in arrays as usual variables!!!  Array is a data structure that represents a collection of the same types of data. myList double[] myList = new double[5]; 3.0 int[] tmp = new int[10]; 0 myList[0] = 3.0; 0 . . . myList[1] = 4.5; myList[2] = 2.0; 0 System.out.println(“tmp[2]: ” + tmp[2]); 0 System.out.println(myList[2]); System.out.println(myList[4]); tmp[9] = tmp[0] + tmp[2] + tmp[4]; System.out.println(“tmp[9]: ” + tmp[9]); 13 16 Example Example myList myList double[] myList = new double[5]; double[] myList = new double[5]; 0 3.0 0 4.5 myList[0] = 3.0; myList[0] = 3.0; myList[1] = 4.5; 0 myList[1] = 4.5; 0 myList[2] = 2.0; myList[2] = 2.0; 0 0 0 0 System.out.println(myList[2]); System.out.println(myList[2]); System.out.println(myList[4]); System.out.println(myList[4]); 14 17 Example Example myArray myList double[] myList = new double[5]; double[] myList = new double[5]; 3.0 3.0 0 4.5 myList[0] = 3.0; myList[0] = 3.0; myList[1] = 4.5; 0 myList[1] = 4.5; 0 myList[2] = 2.0; myList[2] = 2.0; 0 0 0 0 System.out.println(myList[2]); System.out.println(myList[2]); System.out.println(myList[4]); System.out.println(myList[4]); 15 18

  4. Example Using for loops to process arrays  Using for loops to traverse or process array elements  Elements are of the same type and processed repeatedly myList  The size of the array is known double[] myList = new double[5]; 3.0 4.5 myList[0] = 3.0; for (int i=0; i<myList.length; i++) { 2.0 myList[1] = 4.5; // process ith element myList[i] myList[2] = 2.0; 0 System.out.println(myList[i]); 0 System.out.println(myList[2]); } System.out.println(myList[4]); 19 Example Enhanced for Loop (for-each loop)  Traditional for loop int[] array = new int[5]; myList for (int i = 0; i < array.length; i++){ double value = array[i]; double[] myList = new double[5]; 3.0 System.out.println(value); 4.5 myList[0] = 3.0; }  Generalized for loop myList[1] = 4.5; 2.0 myList[2] = 2.0; 0 for (double value: array) // for each value in array 0 System.out.println(value); System.out.println(myList[2]); System.out.println(myList[4]); Syntax: for (elementType value: array) { // Process the value } 20 23 Example Common Array Processing  Initializing array elements  Printing arrays myList  Summing all elements double[] myList = new double[5]; 3.0  Finding largest elements 4.5 myList[0] = 3.0;  Finding smallest index of the largest element myList[1] = 4.5; 2.0 myList[2] = 2.0; 0  Shifting elements 0  Flipping elements System.out.println(myList[2]); System.out.println(myList[4]);  Counting occurrences 21

  5. Trace Program with Arrays Initialize array elements using for loop i=1, value[1] is 1 Initialize an array with random values public class Test { for (int i=0; i<myList.length; i++) { public static void main(String[] args) After the first iteration { myList[i] = Math.random() * 100; int[] values = new int[5]; 0 0 for (int i = 1; i < 5; i++) { 1 1 } values[i] = i + values[i-1]; 0 2 } 3 0 values[0] = values[1] + values[4]; 0 4 } } 28 Trace Program with Arrays Another example of initializing arrays i=2, value[2] is 3 public class Test { public static void main(String[] args) public class Test { After the second iteration { public static void main(String[] args) int[] values = new int[5]; { 0 0 for (int i = 1; i < 5; i++) { int[] values = new int[5]; 1 1 values[i] = i + values[i-1]; for (int i = 1; i < 5; i++) { 2 3 } values[i] = i + values[i-1]; 0 3 values[0] = values[1] + values[4]; } 4 0 } values[0] = values[1] + values[4]; } } } 26 29 Trace Program with Arrays Trace Program with Arrays i=3, values[3] = 6 (3 + 3) Declare array variable, create an array, and assign its reference to values public class Test { public static void main(String[] args) public class Test { After the third iteration { public static void main(String[] args) After the array is created int[] values = new int[5]; { 0 0 for (int i = 1; i < 5; i++) { int[] values = new int[5]; 1 1 0 0 values[i] = i + values[i-1]; for (int i = 1; i < 5; i++) { 2 3 1 0 } values[i] = i + values[i-1]; 6 0 3 2 values[0] = values[1] + values[4]; } 4 0 3 0 } values[0] = values[1] + values[4]; 0 4 } } } 27 30

  6. Trace Program with Arrays Simple Array Algorithms: Finding the Maximum or Minimum i=4, values[4] = 10 (4 + 6)  Problem: finding the maximum/minimum  Algorithm: public class Test { public static void main(String[] args)  Initialize the current maximum/minimum with the starting After the fourth iteration { element int[] values = new int[5]; 0 0 for (int i = 1; i < 5; i++) {  For each of the remaining elements 1 1 values[i] = i + values[i-1]; 3 2  Update the current maximum/minimum with the element if it is } 3 6 values[0] = values[1] + values[4]; larger or smaller 10 4 } int max = myList[0]; } for (int i=1; i<myList.length; i++) { if (max < myList[i]) max = myList[i]; } 31 Trace Program with Arrays Simple Array Algorithms: Finding the smallest index of largest item After this line, values[0] is 11 (1 + 10)  Algorithm:  Initialize current maximum with the starting element and the public class Test { index of current maximum with 0 public static void main(String[] args) {  For each of the remaining elements int[] values = new int[5]; for (int i = 1; i < 5; i++) { 0 11  Update current maximum and index if the element is larger values[i] = i + values[i-1]; 1 1 } 3 2 int max = myList[0]; values[0] = values[1] + values[4]; 3 6 int indexOfMax = 0; } 10 4 for (int i=1; i<myList.length; i++) { } if (max < myList[i]) { max = myList[i]; indexOfMax = i; } } What happens if we use max <= myList[i] ? 32 Simple Array Algorithms: Simple Array Algorithms: Counting Matches computing the sum  Problem: computing the sum of all elements in an array  Problem: counting number of a particular item in an array  Algorithm:  Algorithm:  Initialize the current sum with 0  Initialize a counter  For each of the elements  For each of the elements in the array  Add the element to the sum  Increase the counter when there is a match  Example: counting number of As in a hand of cards int sum = 0; int count = 0; for (int i=0; i<myList.length; i++) { for (int i=0; i<myList.length; i++) { sum += myList[i]; if (myList[i] == 1) } count ++; }  How to compute the average?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend