1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Advertisements

CHAPTER 10 ARRAYS II Applications and Extensions.
TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 9 Arrays.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Chapter 8 Arrays and Strings
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Java Programming: From Problem Analysis to Program Design, 4e
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Java Programming: Chapter 9: Arrays
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Array 1 ARRAY. array 2 Learn about arrays. Explore how to declare and manipulate data into arrays. Understand the meaning of “array index out of bounds.”
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Chapter 8 Multidimensional Arrays
Two Dimensional Array Mr. Jacobs.
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Case Study 2 – Marking a Multiple-choice Test
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multi-Dimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Lecture 9 Objectives Learn about arrays.
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Java Programming: Program Design Including Data Structures
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Presentation transcript:

1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier

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

3 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, , 112.0, 77, 11, 22, 86.62); System.out.println(largest(numberList));

4 Rules to follow when using a variable length formal parameter list: 1.A method can have both a variable length formal parameter and other formal parameter. public static void myMethod (String name, double num, int … intList) 2.A method can have at most one varible length formal parameter. 3.If a method has both a variable length formal parameter and other formal parameter, then the variable length formal parameter must be the last). Arrays and Variable Length Parameter List

5 foreach loop  The most recent version of Java provides a special type of for loop to process the elements of an objects such as an array.  The syntax to use this for loop to process the elements of an array is: for (dataType identifier : arrayName) statements  identifier is a variable, and the data type of identifier is the same as the data type of the array components.  This form of for is called a foreach loop.

6 foreach loop sum = 0; //line1 for (double num : list) //line2 sum = sum + num; //line3  The for statement in Line 2 is read for each num in list. The identifier num is initialized to list[0]. In the next iteration, the value of num is list[1], and so on. for (double num : numList) { if (max < num) max = num; }

7 [Red][Brown][Black][White][Gray] [GM] [Ford]63664 [Toyota]35367 [BMW]82753 [Nissan] [Volvo]15797 Two-Dimensional Arrays inStock

8 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

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

10 Accessing Two-Dimensional Array Components =25.75

11 int [][] matrix = new int[20][15]; 20 rows 15 columns matrix.length = 20 //number of rows  Each row of matrix is 1-D array matrix.[0].length = 15 //# of columns in 1 st row matrix.[1].length = 15 // # of columns in 2 nd row Two-Dimensional Arrays

12 Two-Dimensional Arrays: Special Cases  Can specify different number of columns for each row (ragged arrays). In this case, each row must be instantiated separately.

13 Two-Dimensional Arrays: Special Cases To create this array, first we create 1-D array board of 5 rows: int[] board = new int[5]; board[0] = new int[6]; board[1] = new int[2]; board[2] = new int[2]; board[3] = new int[3]; board[4] = new int[4];

14 Two Dimensional Array Initialization During Declaration int[][] board = {{2,3,1}, {15,25,13}, {20, 4, 7}}; [0][1][2] [0]231 [1] [2]2047 board What is the array created after this statement: int[][] table = {{2,3,1,5}, {15,25}, {4, 23, 45}};

15 Processing Two Dimensional Array  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.  The following declarations are used for our examples: static final int ROWS=7; static final int COLUMNS=6; int[][] matrix = new int [ROWS][COLUMNS]; int row, col, sum, largest, temp;

16  Loop for processing row 5 elements: for (col=0; col < matrix[5].length; col++) //process matrix[5][col]  Loop for processing column 2 elements: for (row=0; row < matrix.length; row++) //process matrix[row][2] Two-Dimensional Arrays: Processing

17 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(); }

18 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); } Two-Dimensional Arrays: Processing

19 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); } Two-Dimensional Arrays: Processing

20 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); } Two-Dimensional Arrays: Processing

21 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); } Two-Dimensional Arrays: Processing

22 Multidimensional Arrays  Can define three-dimensional arrays or n-dimensional arrays (n can be any number).  Syntax to declare and instantiate array: d ataType[][]…[] 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

23 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;

24 Programming Example: Text Processing  Program: Reads given text; outputs the text as is; prints number of lines and number of times each letter appears in text.  Input: File containing text to be processed.  Output: File containing text, number of lines, number of times each letter appears in text.

25 Programming Example Solution: Text Processing  An array of 26 representing the letters in the alphabet.  Three methods:  copyText  characterCount  writeTotal  Value in appropriate index is incremented using methods and depends on character read from text.

26 Chapter Summary  Arrays  Definition  Uses  Different arrays  One-dimensional  Two-dimensional  Multidimensional (n-dimensional)  Arrays of objects  Parallel arrays

27 Chapter Summary  Declaring arrays  Instantiating arrays  Processing arrays  Entire array  Row processing  Column processing  Common operations and methods performed on arrays  Manipulating data in arrays