1 / 83

Java Programming: From Problem Analysis to Program Design, 4e

Java Programming: From Problem Analysis to Program Design, 4e. Chapter 9 Arrays. Chapter Objectives. Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index out of bounds” Become familiar with the restrictions on array processing.

Download Presentation

Java Programming: From Problem Analysis to Program Design, 4e

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 9 Arrays

  2. Chapter Objectives • Learn about arrays • Explore how to declare and manipulate data into arrays • Understand the meaning of “array index out of bounds” • Become familiar with the restrictions on array processing Java Programming: From Problem Analysis to Program Design, 4e

  3. Chapter Objectives (continued) • Discover how to pass an array as a parameter to a method • Discover how to manipulate data in a two-dimensional array • Learn about multidimensional arrays Java Programming: From Problem Analysis to Program Design, 4e

  4. Array • Definition: structured data type with a fixed number of elements • Elements of an array are also called components of the array • Every element is of the same type • Elements are accessed using their relative positions in the array Java Programming: From Problem Analysis to Program Design, 4e

  5. One-Dimensional Arrays Java Programming: From Problem Analysis to Program Design, 4e

  6. One-Dimensional Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e

  7. One-Dimensional Arrays (continued) • intExp = number of components in array >= 0 • 0 <= indexExp <= intExp Java Programming: From Problem Analysis to Program Design, 4e

  8. Arrays Array num:int[] num = new int[5]; Java Programming: From Problem Analysis to Program Design, 4e

  9. Arrays (continued) Array num:int[] num = new int[5]; Java Programming: From Problem Analysis to Program Design, 4e

  10. Array List Java Programming: From Problem Analysis to Program Design, 4e

  11. Array List (continued) Java Programming: From Problem Analysis to Program Design, 4e

  12. Array List (continued) Java Programming: From Problem Analysis to Program Design, 4e

  13. Array List (continued) Java Programming: From Problem Analysis to Program Design, 4e

  14. Specifying Array Size During Program Execution Java Programming: From Problem Analysis to Program Design, 4e

  15. Array Initialization During Declaration • The initializer list containsvalues, called initial values, that are placed between braces and separated by commas • sales[0]= 12.25, sales[1]= 32.50, sales[2]= 16.90, sales[3]= 23.00, and sales[4]= 45.68 Java Programming: From Problem Analysis to Program Design, 4e

  16. Array Initialization During Declaration (continued) • When declaring and initializing arrays, the size of the array is determined by the number of initial values within the braces • If an array is declared and initialized simultaneously, we do not use the operator new to instantiate the array object Java Programming: From Problem Analysis to Program Design, 4e

  17. Arrays and the Instance Variable length • Associated with each array that has been instantiated, there is a public (final) instance variable length • The variable length contains the size of the array • The variable length can be directly accessed in a program using the array name and the dot operator int[] list = {10, 20, 30, 40, 50, 60}; Java Programming: From Problem Analysis to Program Design, 4e

  18. Arrays and the Instance Variable length (continued) • This statement creates the array list of six components and initializes the components using the values given • Here list.length is 6 int[] numList = new int[10]; • This statement creates the array numList of 10 components and initializes each component to 0 Java Programming: From Problem Analysis to Program Design, 4e

  19. Arrays and the Instance Variable length (continued) • The value of numList.length is 10 numList[0] = 5; numList[1] = 10; numList[2] = 15; numList[3] = 20; • These statements store 5, 10, 15, and 20, respectively, in the first four components of numList • You can store the number of filled elements, that is, the actual number of elements, in the array in a variable, say numOfElement • It is a common practice for a program to keep track of the number of filled elements in an array Java Programming: From Problem Analysis to Program Design, 4e

  20. Processing One-Dimensional Arrays • Loops used to step through elements in array and perform operations int[] list = new int[100]; int i; for (i = 0; i < list.length; i++) //process list[i], the (i + 1)th //element of list for (i = 0; i < list.length; i++) list[i] = console.nextInt(); for (i = 0; i < list.length; i++) System.out.print(list[i] + " "); Java Programming: From Problem Analysis to Program Design, 4e

  21. Arrays (continued) • Some operations on arrays • Initialize • Input data • Output stored data • Find largest/smallest/sum/average of elements double[] sales = newdouble[10]; int index; double largestSale, sum, average; Java Programming: From Problem Analysis to Program Design, 4e

  22. Code to Initialize Array to Specific Value (10.00) for (intindex = 0; index < sales.length; index++) sales[index] = 10.00; Java Programming: From Problem Analysis to Program Design, 4e

  23. Code to Read Data into Array for (intindex = 0; index < sales.length; index++) sales[index] = console.nextDouble(); Java Programming: From Problem Analysis to Program Design, 4e

  24. Code to Print Array for (intindex = 0; index < sales.length; index++) System.out.print(sales[index] + " "); Java Programming: From Problem Analysis to Program Design, 4e

  25. Code to Find Sum and Average of Array sum = 0; for (intindex = 0; index < sales.length; index++) sum = sum + sales[index]; if (sales.length != 0) average = sum / sales.length; else average = 0.0; Java Programming: From Problem Analysis to Program Design, 4e

  26. Determining Largest Element in Array maxIndex = 0; for (intindex = 1; index < sales.length; index++) if (sales[maxIndex] < sales[index]) maxIndex = index; largestSale = sales[maxIndex]; Java Programming: From Problem Analysis to Program Design, 4e

  27. Determining Largest Element in Array (continued) Java Programming: From Problem Analysis to Program Design, 4e

  28. Determining Largest Element in Array (continued) Java Programming: From Problem Analysis to Program Design, 4e

  29. Array Index Out of Bounds • Array in bounds if: 0 <= index <= arraySize – 1 • If index < 0 or index > arraySize: ArrayIndexOutOfBoundsException exception is thrown • Base address: memory location of first component in array Java Programming: From Problem Analysis to Program Design, 4e

  30. Declaring Arrays as Formal Parameters to Methods • A general syntax to declare an array as a formal parameter dataType[] arrayName public static void arraysAsFormalParameter(int[] listA, double[] listB, int num) { //... } int[] intList = newint[10]; double[] doubleNumList = newdouble[15]; int number; arraysAsFormalParameter(intList, doubleNumList, number); Java Programming: From Problem Analysis to Program Design, 4e

  31. The Assignment Operators and Arrays Java Programming: From Problem Analysis to Program Design, 4e

  32. The Assignment Operators and Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e

  33. The Assignment Operators and Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e

  34. Relational Operators and Arrays • if (listA == listB)... • - The expression listA == listB determines if the values of listA and listB are the same and thus determines whether listA and listB refer to the same array • - To determine whether listA and listB contain the same elements, you need to compare them component by component • - You can write a method that returns true if two int arrays contain the same elements Java Programming: From Problem Analysis to Program Design, 4e

  35. Relational Operators and Arrays (continued) booleanareEqualArrays(int[] firstArray, int[] secondArray) { if (firstArray.length != secondArray.length) return false; for (int index = 0; index < firstArray.length; index++) if (firstArray[index] != secondArray[index]) return false; return true; } if (areEqualArrays(listA, listB)) ... Java Programming: From Problem Analysis to Program Design, 4e

  36. Arrays as Parameter Methods Java Programming: From Problem Analysis to Program Design, 4e

  37. Methods for Array Processing Java Programming: From Problem Analysis to Program Design, 4e

  38. Methods for Array Processing (continued) Java Programming: From Problem Analysis to Program Design, 4e

  39. Methods for Array Processing (continued) Java Programming: From Problem Analysis to Program Design, 4e

  40. Methods for Array Processing (continued) Java Programming: From Problem Analysis to Program Design, 4e

  41. Suppose that you want to determine whether 27 is in the list; • First you compare 27 with list[0] • Because list[0] ≠ 27, you then compare 27 with list[1] • Because list[1] ≠ 27, you compare 27 with list[2]; Because list[2] = 27, the search stops • This search is successful Java Programming: From Problem Analysis to Program Design, 4e

  42. Search for 10 • Search starts at the first element in the list, that is, at list[0] • This time, the search item, which is 10, is compared with every item in the list; eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search Java Programming: From Problem Analysis to Program Design, 4e

  43. public static int seqSearch(int[] list, int listLength, int searchItem) { int loc; boolean found = false; loc = 0; while (loc < listLength && !found) if (list[loc] == searchItem) found = true; else loc++; if (found) return loc; else return -1; } Java Programming: From Problem Analysis to Program Design, 4e

  44. Arrays of Objects • Can use arrays to manipulate objects • Example: create array named array1 with N objects of type T T[] array1 = new T[N] • Can instantiate array1 as follows: for (int j = 0; j <array1.length; j++) array1[j] = new T(); Java Programming: From Problem Analysis to Program Design, 4e

  45. Array of String Objects String[] nameList = new String[5]; nameList[0] = "Amanda Green"; nameList[1] = "Vijay Arora"; nameList[2] = "Sheila Mann"; nameList[3] = "Rohit Sharma"; nameList[4] = "Mandy Johnson"; Java Programming: From Problem Analysis to Program Design, 4e

  46. Array of String Objects (continued) Java Programming: From Problem Analysis to Program Design, 4e

  47. Arrays of Objects (continued) Clock[] arrivalTimeEmp = new Clock[100]; Java Programming: From Problem Analysis to Program Design, 4e

  48. Instantiating Array Objects for (int j = 0; j < arrivalTimeEmp.length; j++) arrivalTimeEmp[j] = new Clock(); Java Programming: From Problem Analysis to Program Design, 4e

  49. Instantiating Array Objects (continued) arrivalTimeEmp[49].setTime(8, 5, 10); Java Programming: From Problem Analysis to Program Design, 4e

  50. Arrays and Variable Length Parameter List • The syntax to declare a variable length formal parameter (list) is: dataType ... identifier Java Programming: From Problem Analysis to Program Design, 4e

More Related