Chapter 8 Arrays and the arraylist class

49 5 0
Chapter 8   Arrays and the arraylist class

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 8 Chapter 8 Arrays and the ArrayList Class Introduction to Arrays Employee Class 3 Contents I What is an Array? II Processing Array Elements III Passing Arrays As Arguments to Methods IV Some.

Chapter Arrays and the ArrayList Class Introduction to Arrays Employee Class Contents I What is an Array? II Processing Array Elements III Passing Arrays As Arguments to Methods IV Some Useful Array Algorithms and Operations V Returning Arrays from Methods VI The Sequential Search Algorithm I What is an Array? One-Dimensional Arrays Accessing Array Elements Inputting and Outputting Array Contents Java Performs Bounds Checking Watch Out for Off-by-One Errors Array Initialization Alternate Array Declaration Notation I.1 One-Dimensional Arrays   An one-dimensional array (array) is an object that can store a group of value, all of the same type Declaration of an array reference variable numbers int[] numbers;   The numbers variable can reference an array of int values Size of array Creating an array of int values: number = new int[5]; I.1 One-Dimensional Arrays Enough memory to hold one int int count; 1234 Enough memory to hold one double double number; 1234.55 Enough memory to hold one char char letter; A int[] numbers = new int[5]; numbers variable Element Element Element 0 Element Element I.1 One-Dimensional Arrays  Examples:  int[] numbers = new int[5];  float[] temperatures = new float[100]; char[] letters = new char[41]; long[] units = new long[50]; double[] sizes = new double[1200]; final int NUM_ELEMENTS = 5; int[] numbers = new int[NUM_ELEMENTS];  Once an array is created, its size cannot be changed  By default, Java initializes array elements with I.2 Accessing Array Elements   Each element in an array can be accessed through a number known as a subscript A subscript is used as an index to point a specific element within an array   The first element is assigned the subscript 0, the second element is assigned 1, and so forth Each element in an array can be used as a variable int[] numbers = new int[5]; 0 0 numbers variable subscript I.2 Accessing Array Elements  Each element in the array is accessed by its subscript int[] numbers = new int[5]; numbers[0] = 20; numbers[2] = 30; numbers[4] = 40; 20 numbers[1] = 25; numbers[3] = 35; 25 30 35 40 numbers variable numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] I.3 Inputting and Outputting Array Contents 10 35 IV Some Useful Array Algorithms and Operations Comparing Arrays Summing the Values in a Numeric Array Getting the Average of the Values in a Numeric Array Finding the Highest and Lowest Values in a Numeric Array Partially Filled Arrays Working with Arrays and Files 36 IV.1 Comparing Arrays   To compare the contents of two arrays, we must compare the elements of the two arrays We cannot use the == operator to compare two array reference variables and determine the arrays are equal int[] firstArray = { 5, 10, 15 }; int[] secondArray = { 5, 10, 15}; if(firstArray == secondArray) //This is a mistake System.out.println(“The arrays are the same.”); else System.out.println(“The arrays are not the same.”); 37 IV.1 Comparing Arrays  Comparing the contents of two arrays: int[] firstArray = { 5, 10, 15 }; int[] secondArray = { 5, 10, 15}; boolean arrayEqual = true; //Flag variable int index = 0; //Loop control variable //First determine whether the arrays are the same size if(firstArray.length == secondArray.length) arrayEqual = true; while(arrayEqual && index < firstArray.length) { if(firstArray[index] != secondArray[index]) arrayEqual = false; else index++; } if(arrayEqual) System.out.println(“The arrays are equal.”); else System.out.println(“The arrays are not equal.”); 38 IV.2 Summing the Values in a Numeric Array  To sum the values in an array we must use a loop with an accumulator variable  The loop adds the value in each array element to the accumulator int[] units = new int[5]; // int acc = 0; // Initialize accumulator for(int index = 0; index < units.length; index++) acc += units[index]; 39 IV.3 Getting the Average of the Values in a Numeric Array   The first step in calculating the average of all the values in an array is to sum the values The second step is to divide the sum by the number of elements in the array double[] scores = new double[10]; // double acc = 0; // Initialize accumulator double average; //Will hold the average for(int index = 0; index < scores.length; index++) acc += scores[index]; average = acc / scores.length; 40 IV.4 Finding the Highest and Lowest Values in a Numeric Array int[] numbers = new int[50]; // //Find the highest value int highest = numbers[0]; for(int index = 1; index < numbers.length; index++) { if(numbers[index] > highest) highest = numbers[index]; }  How to find the lowest value ? 41 IV.5 Partially Filled Arrays    Sometimes we need to store a series of items in an array, but we not know the number of items that there are One solution is to make the array large enough to hold the largest possible number of items If the actual number of items stored in the array is less than the number of elements, the array will be only partially filled   When we process a partially filled array, we must only process the elements that contain valid data items A partially filled array is normally used with an accompanying integer variable that holds the number of 42 items stored in the array IV.6 Working with Arrays and Files  Saving the contents of an array to a file is a straightforward procedure:  Use a loop to step through each element of the array, writing its contents to the file int[] numbers = { 10, 20, 30}; //Open the file FileWriter fwriter = new FileWriter(“Values.txt”); PrintWriter outputFile = new PrintWriter(fwriter); //Write the array elements to the file for(int index=0; index

Ngày đăng: 27/09/2022, 22:24

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan