Arrays

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Arrays as PDF for free.

More details

  • Words: 1,812
  • Pages: 39
Ar ra ys

Java Programming: From Problem Analysis to Program Design, Second Edition

Learning Objectives  Revise about arrays.  Explore how to declare and manipulate data into arrays.  Become familiar with the restrictions on array processing.  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, Second Edition

2

Array  A structured data type with a fixed number of components.  Every component is of the same type.  Components are accessed using their relative positions in the array.

Java Programming: From Problem Analysis to Program Design, Second Edition

3

One-Dimensional Arrays  Syntax to instantiate an array:  dataType[ arrayName  dataType[  dataType[

] arrayName; = new dataType[intExp] ] arrayName = new dataType[intExp] ] arrayName1, arrayName2;

 Syntax to access an array component:  arrayName[indexExp]  intExp = number of components in array >= 0  0 <= indexExp <= intExp Java Programming: From Problem Analysis to Program Design, Second Edition

4

Array num int[] num = new int[5];

Java Programming: From Problem Analysis to Program Design, Second Edition

5

Array list

Java Programming: From Problem Analysis to Program Design, Second Edition

6

Array Initialization During Declaration double[] sales = {12.25, 32.50, 16.90, 23, 45.68};  The values, called initial values, are placed between braces and separated by commas.  Here, sales[0]= 12.25, sales[1]= 32.50, sales[2]= 16.90, sales[3]= 23.00, and sales[4]= 45.68.  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, Second Edition

7

Arrays and the Instance Variable length  A public instance variable length is associated with each array that has been instantiated.  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.  This statement creates the array list of six components and initializes the components using the values given. Here list.length is 6. int[] list = {10, 20, 30, 40, 50, 60};

Java Programming: From Problem Analysis to Program Design, Second Edition

8

Arrays and the Instance Variable length  This statement creates the array numList of 10 components and initializes each component to 0. int[] numList = new int[10];  The value of numList.length is 10.  These statements store 5, 10, 15, and 20, respectively, in the first four components of numList. numList[0] = 5; numList[1] = 10; numList[2] = 15; numList[3] = 20;  You can store the number of filled elements, that is, the actual number of elements, in the array in a variable, say noOfElement. 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, Second Edition

9

Arrays 

Some operations on arrays:    

Initialize Input data Output stored data Find largest/smallest/sum/average of elements

double[] sales = new double[10]; int index; double largestSale, sum, average;

Java Programming: From Problem Analysis to Program Design, Second Edition

10

Code to Initialize Array to Specific Value (10.00) for (index = 0; index < sales.length; index++) sales[index] = 10.00;

Java Programming: From Problem Analysis to Program Design, Second Edition

11

Code to Read/Input Data into Array for (index = 0; index < sales.length; index++) sales[index] = console.nextDouble();

Code to Print/Output Array for (index = 0; index < sales.length; index++) System.out.print(sales[index] + " ");

Java Programming: From Problem Analysis to Program Design, Second Edition

12

Code to Find Sum and Average of Array sum = 0; for (index = 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, Second Edition

13

Determining Largest Element in Array maxIndex = 0; for (index = 1; index < sales.length; index++) if (sales[maxIndex] < sales[index]) maxIndex = index; largestSale = sales[maxIndex];

Java Programming: From Problem Analysis to Program Design, Second Edition

14

Determining Largest Element in Array

Java Programming: From Problem Analysis to Program Design, Second Edition

15

Methods for Array Processing public static void fillArray(int[] list, int noOfElements) { int index; for (index = 0; index < noOfElements; index++) list[index] = console.nextInt(); }

Java Programming: From Problem Analysis to Program Design, Second Edition

16

Methods for Array Processing public static void printArray(int[] list, int noOfElements) { int index; for (index = 0; index < noOfElements; index++) System.out.print(list[index] + " "); } public static int sumArray(int[] list, int noOfElements) { int index; int sum = 0; for (index = 0; index < noOfElements; index++) sum = sum + list[index]; return sum; } Java Programming: From Problem Analysis to Program Design, Second Edition

17

Methods for Array Processing public static int indexLargestElement(int[] list, int noOfElements) { int index; int maxIndex = 0; for (index = 1; index < noOfElements; index++) if (list[maxIndex] < list[index]) maxIndex = index; return maxIndex; } public static void copyArray(int[] list1, int[] list2, int noOfElements) { int index; for (index = 0; index < noOfElements; index++) list2[index] = list1[index]; } Java Programming: From Problem Analysis to Program Design, Second Edition

18

Array of String Objects String[] nameList = new String[5]; nameList[0] nameList[1] nameList[2] nameList[3]

= = = =

"Amanda Green"; "Vijay Arora"; "Sheila Mann"; "Rohit Sharma";

nameList[4] = "Mandy Johnson";

Java Programming: From Problem Analysis to Program Design, Second Edition

19

Array of String Objects

Java Programming: From Problem Analysis to Program Design, Second Edition

20

Arrays of Objects Clock[] arrivalTimeEmp = new Clock[100];

Java Programming: From Problem Analysis to Program Design, Second Edition

21

Instantiating Array Objects for (int j = 0; j < arrivalTimeEmp.length; j++) arrivalTimeEmp[j] = new Clock();

Java Programming: From Problem Analysis to Program Design, Second Edition

22

Instantiating Array Objects arrivalTimeEmp[49].setTime(8, 5, 10);

Java Programming: From Problem Analysis to Program Design, Second Edition

23

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, Second Edition

24

Arrays and Variable Length Parameter List public static double largest(double ... numList){ double max; int index; if (numList.length != 0){ max = list[0]; for (index = 1; index < numList.length; index++) { if (max < numList [index]) max = numList [index]; } return max; } return 0.0; }

Java Programming: From Problem Analysis to Program Design, Second Edition

25

Arrays and Variable Length Parameter List double num1 = largest(34, 56); double num2 = largest(12.56, 84, 92); double num3 = largest(98.32, 77, 64.67, 56); System.out.println(largest(22.50, 67.78, 92.58, 45, 34, 56)); double[] numberList = {18. 50, 44, 56.23, 17.89 92.34, 112.0, 77, 11, 22, 86.62); System.out.println(largest(numberList));

Java Programming: From Problem Analysis to Program Design, Second Edition

26

Two-Dimensional Arrays  Data is sometimes in table form (difficult to represent using a one-dimensional array).  To declare/instantiate a two-dimensional array: dataType[ ][ ] arrayName = new dataType[intExp1][intExp2];  To access a component of a two-dimensional array: arrayName[indexExp1][indexExp2];  intExp1, intExp2 >= 0  indexExp1 = row position  indexExp2 = column position Java Programming: From Problem Analysis to Program Design, Second Edition

27

Two-Dimensional Arrays  Can specify different number of columns for each row (ragged arrays).  Three ways to process two-dimensional arrays:  Entire array.  Particular row of array (row processing).  Particular column of array (column processing).

 Processing algorithms is similar to processing algorithms of one-dimensional arrays.

Java Programming: From Problem Analysis to Program Design, Second Edition

28

Two-Dimensional Arrays double[][]sales = new double[10][5];

Java Programming: From Problem Analysis to Program Design, Second Edition

29

Accessing Two-Dimensional Array Components

Java Programming: From Problem Analysis to Program Design, Second Edition

30

Two-Dimensional Arrays: Special Cases

Java Programming: From Problem Analysis to Program Design, Second Edition

31

Two-Dimensional Arrays: Processing Initialization for (row = 0; row < matrix.length; row++) for (col = 0; col < matrix[row].length; col++) matrix[row][col] = 10;

Print for (row = 0; row < matrix.length; row++) { for (col = 0; col < matrix[row].length; col++) System.out.printf("%7d", matrix[row][col]);

System.out.println(); } Java Programming: From Problem Analysis to Program Design, Second Edition

32

Two-Dimensional Arrays: Processing Input for (row = 0; row < matrix.length; row++) for (col = 0; col < matrix[row].length; col++) matrix[row][col] = console.nextInt();

Sum by Row for (row = 0; row < matrix.length; row++) { sum = 0; for (col = 0; col < matrix[row].length; col++) sum = sum + matrix[row][col]; System.out.println("Sum of row " + (row + 1) + " = "+ sum);

}

Java Programming: From Problem Analysis to Program Design, Second Edition

33

Two-Dimensional Arrays: Processing Sum by Column for (col = 0; col < matrix[0].length; col++) { sum = 0; for (row = 0; row < matrix.length; row++) sum = sum + matrix[row][col]; System.out.println("Sum of column " + (col + 1) + " = " + sum);

}

Java Programming: From Problem Analysis to Program Design, Second Edition

34

Two-Dimensional Arrays: Processing Largest Element in Each Row for (row = 0; row < matrix.length; row++) { largest = matrix[row][0]; for (col = 1; col < matrix[row].length; col++) if (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println("The largest element of row " + (row + 1) + " = " + largest);

}

Java Programming: From Problem Analysis to Program Design, Second Edition

35

Two-Dimensional Arrays: Processing Largest Element in Each Column for (col = 0; col < matrix[0].length; col++) { largest = matrix[0][col]; for (row = 1; row < matrix.length; row++) if (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println("The largest element of col " + (col + 1) + " = " + largest);

}

Java Programming: From Problem Analysis to Program Design, Second Edition

36

Multidimensional Arrays  Can define three-dimensional arrays or n-dimensional arrays (n can be any number).  Syntax to declare and instantiate array: dataType[][]…[] arrayName = new dataType[intExp1][intExp2]…[intExpn];

 Syntax to access component: arrayName[indexExp1][indexExp2]…[indexExpn]  intExp1, intExp2, ..., intExpn = positive integers  indexExp1,indexExp2, ..., indexExpn = non-negative integers

Java Programming: From Problem Analysis to Program Design, Second Edition

37

Loops to Process Multidimensional Arrays double[][][] carDealers = new double[10][5][7]; For (i = 0; i < 10; i++) for (j = 0; j < 5; j++) for (k = 0; k < 7; k++) carDealers[i][j][k] = 10.00;

Java Programming: From Problem Analysis to Program Design, Second Edition

38

Summary  Arrays: Definition & Uses  Different arrays:  One-dimensional,  Two-dimensional, Multidimensional (ndimensional), Arrays of objects

   

Declaring arrays Instantiating arrays Processing arrays Manipulating data in arrays

Java Programming: From Problem Analysis to Program Design, Second Edition

39

Related Documents

Arrays
November 2019 37
Arrays
May 2020 23
Arrays
November 2019 37
Arrays
November 2019 39
Arrays
November 2019 46