CS2011 Introduction to Programming I Multidimensional Arrays

Slides:



Advertisements
Similar presentations
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.
Advertisements

Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays - The Movie A Farelly Brothers Presentation. No. Not Really.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CS 201 Tarik Booker California State University, Los Angeles.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
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.
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.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Lecture 8: 2D Arrays and Nested Loops
Chapter 8 Multidimensional Arrays
Two-Dimensional Arrays
Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Computer Programming BCT 1113
Two Dimensional Arrays
Chapter 8 Multidimensional Arrays
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 8 Multidimensional Arrays
Arrays An Array is an ordered collection of variables
Chapter 8 Multidimensional Arrays
Chapter 8 Multi-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Introduction To Programming Information Technology , 1’st Semester
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Loop Statements (II)
CS2011 Introduction to Programming I Methods (II)
Multidimensional Arrays
CS2011 Introduction to Programming I Arrays (I)
Chapter 8 Multidimensional Arrays
MSIS 655 Advanced Business Applications Programming
Multidimensional Arrays
Multidimensional array
Arrays Week 2.
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multi-Dimensional Arrays
CHAPTER 2 Arrays and Vectors.
Chapter 8 Multidimensional Arrays
CS2011 Introduction to Programming I Selections (I)
Chapter 7 Multidimensional Arrays
CHAPTER 2 Arrays and Vectors.
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays Section 6.4
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Presentation transcript:

CS2011 Introduction to Programming I Multidimensional Arrays Chengyu Sun California State University, Los Angeles

The Grades Example … Grades Student 1 95 Student 2 80 Student 3 90 Student 4 75 An array is suitable for storing and processing a list of values

… The Grades Example HW1 HW2 HW3 Student 1 95 90 Student 2 80 85 Student 3 88 70 Student 4 75 82 A 2-dimensional array is suitable for storing and processing a table of values

It's Mostly Like An Array … Declare an array variable Array (or 1-Dimensional Array) int[] a; or int a[]; 2-Dimensional Array int[][] a; or int a[][];

… It's Mostly Like An Array … Create an empty array Array (or 1-Dimensional Array) a = new int[10]; 2-Dimensional Array a = new int[10][20];

… It's Mostly Like An Array Access array elements Array (or 1-Dimensional Array) a[0] = 1; 2-Dimensional Array a[0][0] = 1;

What Else Are The Same Array variable and array are two different things Allocated on heap instead of stack Pass-by-reference to methods

Understand Multidimensional Arrays A 2-dimensional array is simply an array of arrays A 3-dimensional array is simply an array of 2-dimensional arrays … … A n-dimensional array is simply an array of (n-1)-dimensional arrays

Array Initializer int[] a = {1, 2, 3, 4}; int[][] b = { {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} }; three 1-d arrays int[][][] c = { { {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} }, { {4, 5, 6, 7}, {5, 6, 7, 8}, {6, 7, 8, 9} } }; two 2-d arrays three 1-d arrays

Ragged Arrays

Array Sizes int[] a = {1, 2, 3, 4}; int[4] int[][] b = { {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} }; int[??][??] int[][][] c = { { {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} }, { {4, 5, 6, 7}, {5, 6, 7, 8}, {6, 7, 8, 9} } }; int[??][??][??]

Array Lengths a.length 4 b.length ?? b[0].length ?? b[1].length ?? c.length ?? c[0].length ?? c[1].length ?? c[0][1].length ?? c[1][2].length ??

Rows and Columns row column 1 2 3 4 5 6 7 8 row column int[][] a = { {1,2}, {3,4}, {5,6}, {7,8} }; // int[4][2] int[][] b = { {1,3,5,7}, {2,4,6,8} }; // int[2][4] column row Row-first (or row-major order) is usually the more natural choice.

Example: Print2DArray Using for loop Using for-each loop

More Array Examples Find the row with the largest sum Using a sumRow() method Find the column with the largest sum Using a sumCol() method

Exercise: Closest Points Find the closest pair of points Distance formula How to get the distance of every pair?

Readings Chapter 8 of the textbook (no quiz next week)