Multi-Dimensional Arrays

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Introduction to C Programming
Two Dimensional Arrays and ArrayList
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional 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.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
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.
© 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.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Arrays.
Arrays Chapter 7.
Chapter 6: Using Arrays.
EGR 2261 Unit 10 Two-dimensional Arrays
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
Chapter Topics Chapter 7 discusses the following main topics:
Topic Dimensional Arrays
Computer Programming BCT 1113
multi-dimensional arrays
Two Dimensional 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.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
ECE Application Programming
Two-Dimensional Arrays
Engineering Problem Solving with C++, Etter/Ingber
Multiple Dimension Arrays
Chapter 7A: Arrays and the ArrayList Class
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 8 Slides from GaddisText
Engr 0012 (04-1) LecNotes
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 7.
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional array
Multidimensional Arrays
Building Java Programs
2D Arrays Just another data structure Typically use a nested loop
INC 161 , CPE 100 Computer Programming
Dr. Sampath Jayarathna Cal Poly Pomona
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
Outline Declaring and Using Arrays Arrays of Objects
Multidimensional Arrays Section 6.4
C++ Array 1.
Programming Fundamentals
Presentation transcript:

Multi-Dimensional Arrays Jan 22, 2014 Multi-Dimensional Arrays These slides are based on the author’s slides

Two-Dimensional Arrays A two-dimensional array is an array of arrays. It can be thought of as having rows and columns. row 0 column 1 column 2 column 3 column 0 row 1 row 2 row 3

Two-Dimensional Arrays Declaring a two-dimensional array requires two sets of brackets and two size declarators The first one is for the number of rows The second one is for the number of columns. double[][] scores = new double[3][4]; The two sets of brackets in the data type indicate that the scores variable will reference a two-dimensional array. Notice that each size declarator is enclosed in its own set of brackets. two dimensional array rows columns

Accessing Two-Dimensional Array Elements When processing the data in a two-dimensional array, each element has two subscripts: one for its row and another for its column.

Accessing Two-Dimensional Array Elements The scores variable holds the address of a 2D array of doubles. column 0 column 1 column 2 column 3 Address row 0 scores[0][0] scores[0][1] scores[0][2] scores[0][3] row 1 scores[1][0] scores[1][1] scores[1][2] scores[1][3] scores[2][0] scores[2][1] scores[2][2] scores[2][3] row 2

Accessing Two-Dimensional Array Elements Accessing one of the elements in a two-dimensional array requires the use of both subscripts. scores[2][1] = 95; The scores variable holds the address of a 2D array of doubles. column 0 column 1 column 2 column 3 Address row 0 row 1 95 row 2

Accessing Two-Dimensional Array Elements Programs that process two-dimensional arrays can do so with nested loops. To fill the scores array: for (int row = 0; row < 3; row++) { for (int col = 0; col < 4; col++) System.out.print("Enter a score: "); scores[row][col] = keyboard.nextDouble(); } Number of rows, not the largest subscript Number of columns, not the largest subscript keyboard references a Scanner object

Accessing Two-Dimensional Array Elements To print out the scores array: for (int row = 0; row < 3; row++) { for (int col = 0; col < 4; col++) System.out.println(scores[row][col]); } See example: CorpSales.java

Initializing a Two-Dimensional Array Initializing a two-dimensional array requires enclosing each row’s initialization list in its own set of braces. int[][] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Java automatically creates the array and fills its elements with the initialization values. row 0 {1, 2, 3} row 1 {4, 5, 6} row 2 {7, 8, 9} Declares an array with three rows and three columns.

Initializing a Two-Dimensional Array int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; produces: The numbers variable holds the address of a 2D array of int values. column 0 column 1 column 2 Address row 0 1 2 3 row 1 4 5 6 7 8 9 row 2

The length Field Two-dimensional arrays are arrays of one-dimensional arrays. The length field of the array gives the number of rows in the array. Each row has a length constant tells how many columns is in that row. Each row can have a different number of columns.

The length Field To access the length fields of the array: int[][] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7 }, { 9, 10, 11, 12 } }; for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) System.out.println(numbers[row][col]); } See example: Lengths.java The array can have variable length rows. Number of rows Number of columns in this row.

Summing The Elements of a Two-Dimensional Array int[][] numbers = { { 1, 2, 3, 4 }, {5, 6, 7, 8}, {9, 10, 11, 12} }; int total; total = 0; for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; } System.out.println("The total is " + total);

Summing The Rows of a Two-Dimensional Array int[][] numbers = {{ 1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int total; for (int row = 0; row < numbers.length; row++) { total = 0; for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; System.out.println("Total of row " + row + " is " + total); }

Summing The Columns of a Two-Dimensional Array int[][] numbers = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int total; for (int col = 0; col < numbers[0].length; col++) { total = 0; for (int row = 0; row < numbers.length; row++) total += numbers[row][col]; System.out.println("Total of column " + col + " is " + total); }

Ragged Arrays int [][] ragged = new int [4][]; When the rows of a two-dimensional array are of different lengths, the array is known as a ragged array. You can create a ragged array by creating a two-dimensional array with a specific number of rows, but no columns. int [][] ragged = new int [4][]; Then create the individual rows. ragged[0] = new int [3]; ragged[1] = new int [4]; ragged[2] = null; ragged[3] = new int [2];

Arrays of Arrays Picture column 0 column 1 column 2 column 3 Address row 0 scores[0][0] scores[0][1] scores[0][2] scores[0][3] row 1 scores[1][0] scores[1][1] scores[1][2] scores[1][3] scores[2][0] scores[2][1] scores[2][2] scores[2][3] row 2

Array of Arrays ragged ragged[0][0] ragged[0][1] ragged[0][2] Address ragged[0][2] ragged[0][] ragged[1][] ragged[1][0] ragged[3][0] null ragged[1][1] ragged[3][1] ragged[3][] ragged[1][2] ragged[3][2] ragged[1][3]

Array of Arrays Object reference Array of object references ragged Address ragged[0][2] ragged[0][] ragged[1][] ragged[1][0] ragged[3][0] null ragged[1][1] ragged[3][1] ragged[3][] ragged[1][2] ragged[3][2] ragged[1][3]

More Than Two Dimensions Java does not limit the number of dimensions that an array may be. More than three dimensions is hard to visualize, but can be useful in some programming problems.