ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.

Slides:



Advertisements
Similar presentations
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Building Java Programs Chapter 7.5
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
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.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/27 Outline Declaring and Using Arrays Arrays of Objects Variable Length.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Dr. Sahar Shabanah Lecture 3: Arrays. Data Structures Data structures are organized collections of information and a set of operations used to manage.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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.
Arrays.
COMP More About Arrays Yi Hong June 05, 2015.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Chapter 9 Nested Loops and Two-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Chapter 9 Introduction to Arrays Fundamentals of Java.
Java Programming Language Lecture27- An Introduction.
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.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Two-Dimensional Arrays
Two-Dimensional Arrays
Nested Loop Review and Two-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Introduction To Programming Information Technology , 1’st Semester
Two-Dimensional Arrays
Outline Declaring and Using Arrays Arrays of Objects
Arrays.
Presentation transcript:

ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays

ECE122 L14: Two Dimensional Arrays March 27, 2007 Outline °Problem: How do store and manipulate data in tabular format °Two-dimensional arrays – easy access with 2 indices °This can get complicated How many references are there to objects? °Arrays as parameters Arrays can be used as input and return values from methods

ECE122 L14: Two Dimensional Arrays March 27, 2007 Two-Dimensional Arrays °Declaration similar to one dimensional arrays °Need to specify both the number of rows and columns during allocation °Example: final int COLS = 6, ROWS = 5; double[][] energyTable = new double[ROWS][COLS]

ECE122 L14: Two Dimensional Arrays March 27, 2007 Two-Dimensional Arrays °A one-dimensional array stores a list of elements °A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions

ECE122 L14: Two Dimensional Arrays March 27, 2007 Two-Dimensional Arrays °  To be precise, in Java a two-dimensional array is an array of arrays °A two-dimensional array is declared by specifying the size of each dimension separately: int[][] scores = new int[12][50]; You may interpret this as an array of 12 rows and 50 columns (short, fat…) °A array element is referenced using two index values: value = scores[3][6] °  The array stored in one row can be specified using one index (scores[3] is a 1d array.)

ECE122 L14: Two Dimensional Arrays March 27, 2007 Arrays of Arrays When we write energyTable = new double[ROWS][COLS]; This is shorthand for energyTable = new double[ROWS][]; for (int i = 0; i < ROWS; i++) energyTable[I] = new double[COLS];

ECE122 L14: Two Dimensional Arrays March 27, 2007 Initializing Two-Dimensional Arrays double[][] energyTable = { {18.9, 19.4, 34.2, 3.9, 5.7, 0.3}, {19.1, 19.3, 33.6, 3.0, 6.2, 0.2}, {18.8, 19.6, 32.9, 3.1, 6.6, 0.2}, {18.9, 20.3, 33.5, 2.8, 6.7, 0.2}, {19.6, 20.8, 33.8, 3.1, 6.5, 0.2} };

ECE122 L14: Two Dimensional Arrays March 27, 2007 Populating energyTable int y, s; // reads 30 numbers needed to fill // energyTable one row at a time for (y = 0; y < ROWS; y++) for (s = 0; s < COLS; s++) energyTable[y][s] = in.readDouble();

ECE122 L14: Two Dimensional Arrays March 27, 2007 Computing Row Totals double [] yearTotals = new double[ROWS]; for (y = 0; y < ROWS; y++) { // compute total for year y yearTotals[y] = 0.0; for (s =0; s < COLS; s++) yearTotals[y] = yearTotals[y] + energyTotal[y][s]; }

ECE122 L14: Two Dimensional Arrays March 27, 2007 Two-Dimensional Arrays ExpressionTypeDescription tableint[][] 2D array of integers, or array of integer arrays table[5]int[] array of integers table[5][12]int integer This can be confusing. Look for the location of the brackets 2D array is really an array of 1D arrays

ECE122 L14: Two Dimensional Arrays March 27, 2007 Formatting Output °The DecimalFormat class can be used to format a floating point value in various ways °For example, you can specify that the number should be truncated to three decimal places °Constructor of the DecimalFormat class takes a string that represents a pattern for the formatted number DecimalFormat fmt = new DecimalFormat (“0.#”); °Check this out in L&L 3.6

ECE122 L14: Two Dimensional Arrays March 27, 2007 public class TwoDArray { // // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. // public static void main (String[] args) { int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; //  What is being loaded into table elements? // Print the table for (int row=0; row < table.length; row++) { for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println(); } // end for } // end main() } // end TwoDArray Make sure you totally understand what is going on here…

ECE122 L14: Two Dimensional Arrays March 27, 2007 Two-Dimensional Arrays of Primitives °Each row in a two-dimensional array is an array °The rows can have different lengths °Defining a primitive array where rows have the same length int [ ][ ] data = new int[3][4]; °Defining a primitive data array where rows have different lengths (ragged array) int [ ][ ] ragged = new int[2][]; ragged[0] = new int[3]; ragged[1] = new int[1];

ECE122 L14: Two Dimensional Arrays March 27, 2007 Arrays Initialization Techniques °Arrays of one-dimensional array Declaring two-dimensional array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; –1 and 2 initialize b[0][0] and b[0][1] –3 and 4 initialize b[1][0] and b[1][1] int b[][] = { { 1, 2 }, { 3, 4, 5 } }; –row 0 contains elements 1 and 2 –row 1 contains elements 3, 4 and 5 °Two-dimensional arrays with rows of different lengths Lengths of rows in array are not required to be the same -E.g., int b[][] = { { 1, 2 }, { 3, 4, 5 } };

ECE122 L14: Two Dimensional Arrays March 27, 2007 Multidimensional Arrays °An array can have many dimensions – if it has more than one dimension, it is called a multidimensional array °Each dimension subdivides the previous one into the specified number of elements °  Each dimension has its own length constant °Because each dimension is an array of array references, the arrays within one dimension can be of different lengths these are sometimes called ragged arrays Only responsible for 1D and 2D arrays in this course

ECE122 L14: Two Dimensional Arrays March 27, D Arrays

ECE122 L14: Two Dimensional Arrays March 27, 2007 Two dimensional array: Review °There are 5 groups in one class °There are 10 students in each group °Their ages are stored in a two dimensional array age[ ][ ] array °Definition : int [ ][ ] age = new int [5][10];

ECE122 L14: Two Dimensional Arrays March 27, 2007 Two dimensional array Notes: age[0][0] refers to 1st student’s age in 1st group age[0][1] refers to 2nd student’s age in 1st group … age[2][8] refers to 9th student’s age in 3rd group … age[4][9] refers to 10th student’s age in 5th group

ECE122 L14: Two Dimensional Arrays March 27, 2007 Summary °We can now make complicated data structures Objects still the basic units of data storage °Arrays are fundamental Most data is stored in arrays Allows for easy data access °Two dimensional arrays allow us to store tables of data In some cases the rows may be of different lengths °Arrays of greater than two dimensions are possible but rarely used