© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Chapter 9: Arrays and Strings
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
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 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
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.
Java Unit 9: Arrays Declaring and Processing Arrays.
IE 212: Computational Methods for Industrial Engineering
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Working with Arrays in MATLAB
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Assembly - Arrays תרגול 7 מערכים.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CS 201 Tarik Booker California State University, Los Angeles.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
Multidimensional Arrays
CS2011 Introduction to Programming I Multidimensional Arrays
Multidimensional Arrays Section 6.4
Presentation transcript:

© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays

© The McGraw-Hill Companies, 2006 Two-dimensional arrays a multi-dimensional array is an array that has more than one index; so far, the arrays that we have shown you have had only one index - for this reason they are very often referred to as one-dimensional arrays; however, an array may have as many indices as is necessary (up to the limit of the memory on your machine); usually, no more than two indices will ever need to be used; an array with two indices is called a two- dimensional array.

© The McGraw-Hill Companies, 2006 Creating a two-dimensional array To create a two-dimensional (2D) array, simply provide the size of both indices. Example Assume we need to record temperatures for four weeks - each week will itself consist of seven temperatures (one for each day of the week):

© The McGraw-Hill Companies, 2006 Accessing elements in a 2D array The name of each item in a two-dimensional array is the array name, plus the row and column index.

© The McGraw-Hill Companies, 2006 Use a pair of nested loops to process a 2D array

© The McGraw-Hill Companies, 2006 Revisiting the 'length' attribute in a 2D array, the length attribute returns the length of the first index (this is what we have visualised as a the number of rows); the number of columns is determined by obtaining the length of a particular row; in the previous example we have chosen the first row but we could have chosen any row here.

© The McGraw-Hill Companies, 2006 Initializing 2D arrays As with one dimensional arrays, it is possible to declare and initialize a multi-dimensional array with a collection of values all in one line. Example

© The McGraw-Hill Companies, 2006 Ragged arrays A two-dimensional array with a variable number of columns is called a ragged array. Example

© The McGraw-Hill Companies, 2006 Creating a ragged array to declare such a ragged array we first need to specify the number of rows and leave the number of columns unspecified: now, for each row we can fix the appropriate size of the associated column; in the example above the first row has 2 columns, the second row 4 columns.

© The McGraw-Hill Companies, 2006 The NoughtsAndCrosses game

© The McGraw-Hill Companies, 2006

Creating a 2D array of buttons A 2D array of buttons would be a good way to implement the Noughts and Crosses board.

© The McGraw-Hill Companies, 2006 Creating the buttons Now we need to allocate a new JButton object to each cell in the array ; give each button an actionListener and then to add each button to a panel (called board say).

© The McGraw-Hill Companies, 2006 Processing each button press

© The McGraw-Hill Companies, 2006 Check if player has won There are four ways in which a player can win a game: the player completes a row; the player completes a column; the player completes the left diagonal; the player completed the right diagonal.

© The McGraw-Hill Companies, 2006 Completing a row When a player wins the game by completing a row, the row index for each winning cell remains the same.

© The McGraw-Hill Companies, 2006 Code for checking a column If a player wins a game by completing a column, then the column index will be fixed to the current column (colIn):

© The McGraw-Hill Companies, 2006 Completing a left diagonal When a player has won the game by completing a left diagonal, the row and column index for each winning cell is the same.

© The McGraw-Hill Companies, 2006 Code for checking a left diagonal

© The McGraw-Hill Companies, 2006 Code for right diagonal When checking the right diagonal, the cells to check are cell[2][0], cell[1][1] and cell[0][2]. This row index can be arrived at by subtracting the column index from 2.