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.

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

Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
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.
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 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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
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:
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
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.
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 =
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 11: Arrays and Vectors 1 Chapter 11 Arrays and Vectors.
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.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
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.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
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];
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
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 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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
Slides by Evan Gallagher
A final variable is a constant
CSC111 Quick Revision.
Principles of Computer Science I
Chapter 7 – Arrays and Array Lists
Chapter 8 – Arrays and Array Lists
7.6 Common Array Algorithm: Filling
An Introduction to Java – Part I, language basics
6.2 for Loops Example: for ( int i = 1; i
Presentation transcript:

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 (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 © 2008 by John Wiley & Sons. All rights reserved. 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: board = 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: board[i][j] = " "; 16: } 17: 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: */ ch06/twodim/TicTacToe.java Continued

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 24: public void set(int i, int j, String player) 25: { 26: if (board[i][j].equals(" ")) 27: board[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: */ 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"; ch06/twodim/TicTacToe.java (cont.) Continued

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 46: } 47: return r; 48: } 49: 50: private String[][] board; 51: private static final int ROWS = 3; 52: private static final int COLUMNS = 3; 53: } ch06/twodim/TicTacToe.java (cont.)

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 01: import java.util.Scanner; 02: 03: /** 04: This program runs a TicTacToe game. It prompts the 05: user to set positions on the board and prints out the 06: result. 07: */ 08: public class TicTacToeRunner 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: { 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: { ch06/twodim/TicTacToeRunner.java Continued

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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: } ch06/twodim/TicTacToeRunner.java (cont.)

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 ch06/twodim/TicTacToeRunner.java (cont.)

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. How do you declare and initialize a 4-by-4 array of integers? Self Check 6.11

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. How do you count the number of empty spaces in the tic-tac-toe board? Self Check 6.12

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Copying an array variable yields a second reference to the same array double[ ] data = new double[10]; // fill array... double[ ] prices = data; Copying Arrays: Copying Array References

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. System.arraycopy(from, fromStart, to, toStart, count); Copying Arrays: Copying Array Elements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. System.arraycopy(data, i, data, i + 1, data.length - i - 1); data[i] = x; Adding an Element to an Array

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. System.arraycopy(data, i + 1, data, i, data.length - i - 1); Removing an Element from an Array

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. If the array is full and you need more space, you can grow the array: Create a new, larger array: double[] newData = new double[2 * data.length]; Copy all elements into the new array: System.arraycopy(data, 0, newData, 0, data.length); Store the reference to the new array in the array variable: data = newData; Growing an Array

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Why do we double the length of the array when it has run out of space rather than increasing it by one element? Self Check 6.14

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming convention: final int DATA_LENGTH = 100; double[] data = new double[DATA_LENGTH]; int data>Size = 0; Update dataSize as array is filled: data[dataSize] = x; dataSize++; Partially Filled Arrays

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Partially Filled Arrays (cont.)