When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =

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

1 Arrays in JavaScript Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0]
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Chapter 7 Arrays and Array Lists. Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD.
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7  Arrays and Array Lists 1 Chapter 7 Arrays and Array Lists.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
Building Java Programs Chapter 7.5
Components of Computers CSH. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Schematic Diagram of a Computer.
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.
Java Programming Week 6: Array and ArrayList Chapter 7.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
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.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 6 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Decisions Bush decision making.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
Arrays.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
COMP More About Arrays Yi Hong June 05, 2015.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
© 2006 Pearson Addison-Wesley. All rights reserved Arrays of Greater Dimension One-dimensional arrays are linear containers. Multi-dimensional Arrays.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Lesson 5-4 Example Example 1 Draw an array to model and find 21 ÷ 3. 1.Write the answer if you know it. Otherwise, draw an array.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Two-Dimensional Arrays
Chapter 7 – Arrays and Array Lists
Chapter 8 – Arrays and Array Lists
Nested Loop Review and Two-Dimensional Arrays
Use a variable to store a value that you want to use at a later time
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
6.2 for Loops Example: for ( int i = 1; i
Parameter: an input to a method
Presentation transcript:

When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board = new String[ROWS][COLUMNS]; Access elements with an index pair: board[1][1] = "x"; board[2][1] = "o"; Two-Dimensional Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1

It is common to use two nested loops when filling or searching: for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLUMNS; j++) board[i][j] = " "; Traversing Two-Dimensional Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 2

You can also recover the array dimensions from the array variable: board.length is the number of rows board[0].length is the number of columns Rewrite the loop for filling the tic-tac-toe board: for (int i = 0; i < board.length; i++) for (int j = 0; j < board[0].length; j++) board[i][j] = " "; Traversing Two-Dimensional Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 3

Animation 7.3: Traversing a Nested Loop in a 2D Array 4

1 /** 2 A 3 x 3 tic-tac-toe board. 3 */ 4 public class TicTacToe 5 { 6 private String[][] board; 7 private static final int ROWS = 3; 8 private static final int COLUMNS = 3; 9 10 /** 11 Constructs an empty board. 12 */ 13 public TicTacToe() 14 { 15 board = new String[ROWS][COLUMNS]; 16 // Fill with spaces 17 for (int i = 0; i < ROWS; i++) 18 for (int j = 0; j < COLUMNS; j++) 19 board[i][j] = " "; 20 } 21 ch07/twodim/TicTacToe.java Continued Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 5

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. How do you declare and initialize a 4-by-4 array of integers? Answer: int[][] array = new int[4][4]; Self Check

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. How do you count the number of spaces in the tic-tac-toe board? Answer: int count = 0; for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLUMNS; j++) if (board[i][j] == ' ') count++; Self Check