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:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
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.
Chapter 7 – Arrays.
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.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Java Programming Week 6: Array and ArrayList Chapter 7.
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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:
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
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.
Chapter 4: Control Structures II
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
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);
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.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
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. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
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.
Arrays Chapter 7.
Values vs. References Lecture 13.
CSC111 Quick Revision.
Principles of Computer Science I
Chapter 8 – Arrays and Array Lists
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 6 More Conditionals and Loops
Computer Programming Methodology Input and While Loop
Repetition-Sentinel,Flag Loop/Do_While
An Introduction to Java – Part I
Conditional Loops.
CSS161: Fundamentals of Computing
CSC 113 Tutorial QUIZ I.
Nested Loop Review and Two-Dimensional Arrays
An Introduction to Java – Part I, language basics
Can store many of the same kind of data together
Self study.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
CSE 143 Lecture 18 More Recursive Backtracking
Methods/Functions.
How do you do the following?
A type is a collection of values
Computer Science Club 1st November 2019.
Presentation transcript:

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 game boards such as in chess or TicTacToe. Additionally, a 2-D array is much like a grid of streets in a city.

When constructing a two-dimensional array, you specify how many rows and columns you need: You access elements with an index pair a[i][j] final int ROWS = 3; final int COLUMNS = 3; String[][] board = new String[ROWS][COLUMNS]; tttBoard[i][j] = “X"; ****Remember that the index of an array starts at zero. ***This code will place an X at whatever location I and j represent.

 Again, a 2-D represents data that corresponds to a grid  An element is referred to by its row and column. The tttBoard[1][2] element stores an X:

 Declaration includes the type followed by two sets of brackets ([][])  The number of rows is determined with a statement similar to: arrayName.length  The number of columns is determined with a statement similar to: arrayName[0].length  Nested for loops are often used to access the elements of a two-dimensional array

An example of a nested for loop Nothing more than a loop inside of a loop One loop accesses row and the other accesses columns for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLUMNS; j++) tttBoard[i][j] = " ";

File TicTacToe.java 01: /** 02: A 3 x 3 tic-tac-toe board. 03: */ 04: public class TicTacToe 05: { 06: /** 07: Constructs an empty board. 08: */ 09: public TicTacToe() 10: { 11: tttBoard = new String[ROWS][COLUMNS]; 12: // Fill with spaces 13: for (int i = 0; i < ROWS; i++) 14: for (int j = 0; j < COLUMNS; j++) 15: tttBoard[i][j] = " "; 16: } 17: Continued…

File TicTacToe.java 18: /** 19: 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") 23: */ 24: public void set(int i, int j, String player) 25: { 26: if (tttBoard[i][j].equals(" ")) 27: tttBoard[i][j] = player; 28: } 29: 30: /** 31: Creates a string representation of the board, such as 32: |x o| 33: | x | 34: | o| the string representation 36: */ Continued…

File TicTacToe.java 37: public String toString() 38: { 39: String r = ""; 40: for (int i = 0; i < ROWS; i++) 41: { 42: r = r + "|"; 43: for (int j = 0; j < COLUMNS; j++) 44: r = r + board[i][j]; 45: r = r + "|\n"; 46: } 47: return r; 48: } 49: 50: private String[][] tttBoard; 51: private static final int ROWS = 3; 52: private static final int COLUMNS = 3; 53: }

File TicTacToeTester.java 01: import java.util.Scanner; 02: 03: /** 04: This program tests the TicTacToe class by prompting the 05: user to set positions on the board and printing out the 06: result. 07: */ 08: public class TicTacToeTester 09: { 10: public static void main(String[] args) 11: { 12: Scanner in = new Scanner(System.in); 13: String player = "x"; 14: TicTacToe game = new TicTacToe(); 15: boolean done = false; 16: while (!done) 17: { Continued…

File TicTacToeTester.java 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: } Continued…

Output | | | | | | 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