Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.

Slides:



Advertisements
Similar presentations
Two Dimensional Arrays and ArrayList
Advertisements

Arrays.
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Introduction to Programming with C++ Fourth Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
Chapter 7: Arrays and the ArrayList Class
© 2012 Pearson Education, Inc. All rights reserved. Lecture 2 (Chapter 8): Arrays and the ArrayList Class Starting Out with Java: From Control Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Chapter 7: Arrays and the ArrayList Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays and the ArrayList Class Starting Out with Java From Control.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8: Arrays and the ArrayList Class Starting Out with Java: From.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CS 201 Tarik Booker California State University, Los Angeles.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
Arrays Chapter 7.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Chapter Topics Chapter 7 discusses the following main topics:
multi-dimensional arrays
Two Dimensional Arrays
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Chapter 7: Arrays.
Nested Loop Review and Two-Dimensional Arrays
Multidimensional Arrays Vectors of Vectors
Chapter 7A: Arrays and the ArrayList Class
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 8 Slides from GaddisText
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
Arrays Chapter 7.
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional Arrays
Multi-Dimensional Arrays
Dr. Sampath Jayarathna Cal Poly Pomona
Multidimensional Arrays Section 6.4
C++ Array 1.
Programming Fundamentals
Presentation transcript:

Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays

2 Contents I. Two-Dimensional Arrays II. Arrays with Three or More Dimensions

3 I. Two-Dimensional Arrays 1. Creating a Two-Dimensional Array 2. Initializing a Two-Dimensional Array 3. The length Field in a Two-Dimensional Array 4. Displaying All the Elements of a Two- Dimensional Arrays 5. Summing All the Elements of a Two- Dimensional Array 6. Summing the Rows of a Two-Dimensional Array

4 I. Two-Dimensional Arrays 7. Summing the Columns of a Two-Dimensional Array 8. Passing Two-Dimensional Arrays to Methods 9. Ragged Arrays

5 I.1. Creating a Two-Dimensional Array Two-dimensional array, which are sometimes called 2D arrays, is an array of arrays. It has rows and columns of elements. Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3

6 I.1. Creating a Two-Dimensional Array To declare a two-dimensional array: double[][] scores = new double[3][4]; Two set of brackets Two size declarators The first one is for the number of rows: 3 The second one is for the number of columns: 4 Two sets of brackets indicate a two-dimensional array Number of rows Number of columns

7 I.1. Creating a Two-Dimensional Array In the scores array: The elements in row 0: scores[0][0]scores[0][1] scores[0][2]scores[0][3] The elements in row 1: scores[1][0]scores[1][1] scores[1][2]scores[1][3] The elements in row 2: scores[2][0]scores[2][1] scores[2][2]scores[2][3]

8 I.1. Creating a Two-Dimensional Array Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 address The scores variable

9 I.1. Creating a Two-Dimensional Array Programs that process two-dimensional arrays can do so with nested loops. final int ROWS = 3; final int COLS = 4; double[][] scores = new double[ROWS][COLS]; for(int row = 0; row < ROWS; row++) { for(int col = 0; col < COLS; col ++) { System.out.print(“Enter a score: “); scores[row][col] = keyboard.nextDouble(); } }

10 I.2. Initializing a Two-Dimensional Array Enclose each row's initialization list in its own set of braces: int[][] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Row 0Row 1Row 2

11 I.3. The length Field in a Two- Dimensional Array A two-dimensional array can be considered as an array of one-dimensional arrays. int[][] numbers = new int[3][4]; addres s numbers[1][1]numbers[1][2]numbers[1][3]numbers[1][0] numbers[0][1]numbers[0][2]numbers[0][3]numbers[0][0] numbers[2][1]numbers[2][2]numbers[2][3]numbers[2][0] numbers[ 0] numbers[ 1] numbers[ 2] address The numbers variable numbers.length 3 numbers[0].length 4 numbers[1].length 4 numbers[2].length 4

12 I.3. Displaying All the Elements of a Two-Dimensional Array int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; //Display all the elements in the array for(int row = 0; row < 3; row++) { for(int col = 0; col < 4; col++) System.out.println(numbers[row][col]); } //A better approach to display all elements in the array for(int row = 0; row < numbers.length; row++) { for(int col = 0; col < numbers[row].length; col++) System.out.println(numbers[row][col]); }

13 I.4. Summing All the Elements of a Two-Dimensional Array int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; int acc = 0; //Accumulator, set to 0 //Sum the array elements for(int row = 0; row < numbers.length; row++) { for(int col = 0; col < numbers[row].length; col++) acc += numbers[row][col]; } //Display the sum System.out.println(“The total is ” + acc);

14 I.5. Summing the Rows of a Two- Dimensional Array Calculate the sum of each row in a two- dimensional array: int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; int acc; //Accumulator for(int row = 0; row < numbers.length; row++) { //Set the accumulator to 0 acc = 0; //Sum a row for(int col = 0; col < numbers[row].length; col++) acc += numbers[row][col]; //Display the sum System.out.println(“The total of row is ” + acc); }

15 I.6. Summing the Column of a Two-Dimensional Array The outer loop controls the column subscript and the inner loop controls the row subscript. int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; int acc; //Accumulator for(int col = 0; col < numbers[0].length; col++) { //Set the accumulator to 0 acc = 0; //Sum a column for(int row = 0; row < numbers.length; row++) acc += numbers[row][col]; //Display the sum System.out.println(“The total of column is ” + acc); }

16 I.7. Passing Two-Dimensional Arrays to Methods When a two-dimensional array is passed to a method, the parameter must be declared as a reference to a two-dimensional array. public static void showArray(int[][] array) The method's parameter, array, is declared as a reference to a two- dimensional array.

17 I.8. Ragged Arrays When the rows of a two-dimensional array are of different lengths, the array is known as a ragged array. Creating a ragged array: //Creating a two-dimensional array, the array has four //rows, but the rows have not yet been created. int[][] ragged = new int[4][]; //Creat the individual rows ragged[0] = new int[3]; //row 0 has 3 elements ragged[1] = new int[4]; //row 1 has 4 elements ragged[2] = new int[5]; //row 2 has 5 elements ragged[3] = new int[6]; //row 3 has 6 elements

18 I.8. Ragged Arrays Processing elements of a ragged array: for(int row = 0; row < ragged.length; row++) { for(int col = 0; col < ragged[row].length; col++) { // Processing the element ragged[row][col] // } }

19 II. Arrays with Three or More Dimensions Java does not limit the number of dimensions that an array may have. A three-dimensional array declaration: double[][][] array = new double[3][5][7]; This array can be though of as three sets of five rows. Each row contains seven elements.