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
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

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.
Chapter 7: Arrays and Array Lists. To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Chapter 13 ARRAY LISTS AND ARRAYS. CHAPTER GOALS To become familiar with using array lists to collect objects To learn about common array algorithms To.
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.
Chapter 13 ARRAY LISTS AND ARRAYS CHAPTER GOALS –To become familiar with using array lists to collect objects –To learn about common array algorithms –To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
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.
Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Chapter 7 – Arrays.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Arrays Chapter 6 Chapter 6.
Chapter 7  Arrays and Array Lists 1 Chapter 7 Arrays and Array Lists.
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.
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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 Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
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];
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
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.
Slides by Evan Gallagher
A final variable is a constant
CSC111 Quick Revision.
Chapter 7 – Arrays and Array Lists
Chapter 8 – Arrays and Array Lists
Chapter 6 More Conditionals and Loops
Computer Programming Methodology Input and While Loop
Chapter Three - Implementing Classes
Nested Loop Review and Two-Dimensional Arrays
Chapter 5 – Decisions Big Java by Cay Horstmann
6.2 for Loops Example: for ( int i = 1; i
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"; Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 7.8 Two-Dimensional Arrays

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.

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.

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.

22 /** 23 Sets a field in the board. The field must be unoccupied. i the row index j the column index player the player ("x" or "o") 27 */ 28 public void set(int i, int j, String player) 29 { 30 if (board[i][j].equals(" ")) 31 board[i][j] = player; 32 } 33 ch07/twodim/TicTacToe.java (cont.) Continued Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

35 Creates a string representation of the board, such as 36 |x o| 37 | x | 38 | o | the string representation 40 */ 41 public String toString() 42 { 43 String r = ""; 44 for (int i = 0; i < ROWS; i++) 45 { 46 r = r + "|"; 47 for (int j = 0; j < COLUMNS; j++) 48 r = r + board[i][j]; 49 r = r + "|\n"; 50 } 51 return r; 52 } 53 } ch07/twodim/TicTacToe.java (cont.) Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

1 import java.util.Scanner; 2 3 /** 4 This program runs a TicTacToe game. It prompts the 5 user to set positions on the board and prints out the 6 result. 7 */ 8 public class TicTacToeRunner 9 { 10 public static void main(String[] args) 11 { 12 Scanner in = new Scanner(System.in); 13 String player = "x"; 14 TicTacToe game = new TicTacToe(); ch07/twodim/TicTacToeRunner.java Continued Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

15 boolean done = false; 16 while (!done) 17 { 18 System.out.print(game.toString()); 19 System.out.print( 20 "Row for " + player + " (-1 to exit): "); 21 int row = in.nextInt(); 22 if (row < 0) done = true; 23 else 24 { 25 System.out.print("Column for " + player + ": "); 26 int column = in.nextInt(); 27 game.set(row, column, player); 28 if (player.equals("x")) 29 player = "o"; 30 else 31 player = "x"; 32 } 33 } 34 } 35 } ch07/twodim/TicTacToeRunner.java (cont.) Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Program Run: | Row for x (-1 to exit): 1 Column for x: 2 | | x| | Row for o (-1 to exit): 0 Column for o: 0 |o | | x| | Row for x (-1 to exit): -1 ch07/twodim/TicTacToeRunner.java (cont.) 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 7.19

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 7.20