Two –Dimensional Arrays Mrs. C. Furman September 18, 2008.

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

Arrays part 3 Multidimensional arrays, odds & ends.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
©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.
Multiple-Subscripted Array
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Chapter 8 Arrays and Strings
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Java Unit 9: Arrays Declaring and Processing Arrays.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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 =
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Two –Dimensional Arrays Mrs. C. Furman Java Programming November 19, 2008.
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:
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
***** SWTJC STEM ***** Chapter 7 cg 68 What Are Arrays? An array is a simple but powerful way to organize and store large amounts of data and information.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
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];
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Chapter 8: Part 3 Collections and Two-dimensional arrays.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CS 201 Tarik Booker California State University, Los Angeles.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Repeating Instructions And Advance collection
Engineering Problem Solving with C++, Etter/Ingber
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
CSCI N207 Data Analysis Using Spreadsheet
Chapter 7 Part 2 Edited by JJ Shepherd
Lecture 13: Two-Dimensional Arrays
Multidimensional array
INC 161 , CPE 100 Computer Programming
Chapter 8 Multidimensional Arrays
2-Dimensional Lists (Matrices) in Python
Building Java Programs
Presentation transcript:

Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

The “for each” loop  A shortcut with for loops.  Used to iterate through a sequence of elements in an array or ArrayList.  The body of the loop is executed for each element in the array or ArrayList.  At the beginning of each iteration, the next element is assigned to the control variable.  Works the same as a standard for loop that loops through all elements of the data structure.  The control variable in “for each” is assigned to the value at each index, where the standard for loop is being assigned the index.

The “for each” loop structure for (Type variable : collection) statementsExample: int [] myList = {1, 3, 5, 7, 9}; int sum = 0; for (int i : myList) sum += i; sum += i;

Example 1  Write a “for each” loop that prints all elements in the array data.

Example 2  Re-write the following loop using for each: list is an ArrayList of Double. List mylist = new ArrayList (); for (int i = 0; i < mylist.size(); i++) for (int i = 0; i < mylist.size(); i++) { System.out.println (mylist.get(i)); System.out.println (mylist.get(i)); }

Two Dimensional Arrays  rows and columns, also called a Matrix  we must specify the number of row and columns we will need when we construct the 2DArray. Similar to Arrays. Example: int [][] magicSquare = new int [4][4];

Accessing Elements in a 2D Array  Elements are indexed [row][column] magicSquare [0][0] = 3; magicSquare [1][3] = 5; int myNum = magicSquare [1][3];

Initializer List for 2D int [][]cars = {{10, 7, 12, 10, 4}, {18, 11, 15, 17, 10}, {18, 11, 15, 17, 10}, {12, 10, 9, 5, 12}, {12, 10, 9, 5, 12}, {16, 6, 13, 8, 3}}; {16, 6, 13, 8, 3}}; GM Ford Toyota BMW red brown black white gray

Getting Size .length gives us the number of rows… then when we access a specific row, we do.length to get number of columns.  int[][] list = {{1,2,3, 4}, {1}, {1, 2, 3}};  int numRows = list.length;//3  int firstCol = list[0].length;//4  int secCol = list[1].length;//1  int thirdCol = list[2].length;//3

Looping Through a 2D Array Example 3: Write a nested for loop to output all elements in the 2D array.

Multiplication Table  Write a loop that will assign a 5 x 5 2D Array to the multiplication tables of The result will look like the below 2D Array