CS 201 Tarik Booker California State University, Los Angeles.

Slides:



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

Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Chapter 9 Introduction to Arrays
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.
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 in Java Corresponds with Chapter 6 of textbook.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
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.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
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.
Single Dimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
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
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
CS2011 Introduction to Programming I Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multi-Dimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays Section 6.4
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Presentation transcript:

CS 201 Tarik Booker California State University, Los Angeles

What we will cover…  2D Array Basics  Obtaining the length of Two-Dimensional Arrays  Ragged Arrays  Processing Two-Dimensional Arrays  Passing Two-Dimensional Arrays to Methods  Multidimensional Arrays

Two-Dimensional Array Basics  So far, we’ve covered single dimensional arrays  Now we cover two (or more) dimension arrays We will start with 2 dimensions, and talk about more…

Two-Dimensional Array Basics (2)  Why 2D arrays? You may have information that is easier to think of in a 2D fashion ○ Grid ○ Plane ○ Board Tic Tac Toe 2D arrays are simply a more convenient way of visualizing information ○ 2D’s are equivalent to 1D’s

Declaring a Two-Dimensional Array Variable  Syntax for two-dimensional array variable: elementType[][] arrayRefVar; Ex: ○ int[][] matrix;  Creating the array matrix = new int[5][5];

Creating the array  Multidimensional array variables also use zero-based indexing Array of n by m elements 0n 0 m

Accessing the Array  To access a two-dimensional (or more) array, simply use put brackets around each element Ex:int[][] matrix = new int[5][5]; Accessing: ○ matrix[2][1] = 7; Sets the element at row 2, by column 1 to 7

Obtaining the Length of Arrays  A two-dimensional array is just a single dimensional array of other single dimensional arrays There is the length of total number of subarrays There is also the length of each subarray If a two-dimensional array has rows of different lengths, it is called a ragged array

Ragged Arrays  Arrays that have different row lengths Ex: int[][] triangleArray = { {1,2,3}, {2,3}, {3} }; This array has different row sizes

Ragged Arrays (2)  You can also create a ragged array with this syntax: int[][] triangleArray = new int[3][]; triangleArray[0] = new int[3]; triangleArray[1] = new int[2]; triangleArray[2] = new int[1];  You can assign values to the array: triangleArray[0][2] = 50; triangleArray[2][0] = 12;

Processing Two-Dimensional Arrays  We used a for loop to process single dimensional arrays  We will also now use nested for loops to process two-dimensional arrays Ex:Initializing arrays with random values for(int row=0; i<matrix.length; row++) { for(int column = 0; column < matrix[row].length; column++) { matrix[row][column] = Math.random() * 100; }

Printing Two-Dimensional Arrays  You can also use nested for loops to print out the contents of each array for(int row=0; i<matrix.length; row++) { for(int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column]+” “); }  What are some other things you can do? ○ Sum all elements ○ Random shuffling

Passing Two-Dimensional Arrays to Methods  You pass a two-dimensional array to method similarly to a single dimensional array Pass the reference to the array When defining the method, make sure you include the dimension of the array ○ Ex: sum method (p294) public static int sum(int[][] m){ … }

Examples  There are many examples in the book Nearest points Sudoku Etc… How would you use this?

Multidimensional Arrays  Arrays are not limited to 2 dimensions, You can declare arrays of 2,3,5,…,n dimensions They are handled just like 2-dimensional arrays ○ Just account for the extra dimension ○ There are no limits (but memory space)