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.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
Arrays part 3 Multidimensional arrays, odds & ends.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 More on 1-D Arrays Overview l Array as a return type to methods l Array of Objects.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Modelling 1-D Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing Array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Building Java Programs Chapter 7.5
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 More on 1-D Array Overview l Array as a return type to methods l Array of Objects l Array Cloning l Preview: 2-D Arrays.
1 One-Dimensional Arrays  What are and Why 1-D arrays?  1-D Array Declaration  Accessing elements of a 1-D Array  Initializer List  Passing Array.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
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.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
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.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays.
Multidimensional Arrays Computer and Programming.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3.
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
Programming application CC213
Yong Choi School of Business CSU, Bakersfield
Java How to Program, Late Objects Version, 10/e
Multidimensional Arrays
Chapter 8 Slides from GaddisText
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays Section 6.4
Presentation transcript:

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 List l Examples using 2-D array

2 Why Multi-Dimensional Arrays? l The same reason that necessitated the introduction of 1-D array can also be extended to Multi-dimensional Array. l For example, to store the grades of (30) students, each of which is taking a number of courses (5 say), we would either use 30 1-D arrays, one for each student or 5 1-D arrays, one for each course l Multi-Dimensional array allows us to handle all these using a single identifier. l 2-Dimensional array is the most commonly used multi-dimensional arrays. l Other multi-dimensional arrays are also possible. For example, the students grades representation example above can be extended to cover several sections by introducing another dimension. Java allows the handling of array of any dimension in a consistent manner.

3 Multi-Dimensional Array Declaration l A two-dimensional array is declared in a similar manner to 1-D array but with the addition of one more bracket as in the following example; int [][] a = new int[3][4]; Although it is convenient to think of a 2-D array as a table as shown above, in reality, a is simply an array of arrays. The following figure shows how a is actually represented. a

4 Multi-Dimensional Array Declaration l The declaration in the previous slide is infact a three-step process. l Creation of the reference a: int[][] a; l Creation of the array of references to arrays a = new int[3][]; l Creation of the individual arrays for (int i=0; i<a.length; i++) a[i] = new int[4];

5 Multi-Dimensional Array Declaration From the declaration of 2-D array above, we can observe that a.length gives the length of the array referenced by a – 3 in this example or the number of rows of the array. To get the number of columns in a, we have to ask how many elements there are in a row; this number would be given by a[0].length, or equivalently by a[1].length or a[2].length l In fact, because the columns are treated individually, they do not need to all be of the same size. l For example, we could have the following : a[0] = new int[4]; a[1] = new int[2]; a[2] = new int[3];

6 Accessing individual elements l We can access individual elements of a 2-D array by specifying both the row and column index in the form: a[i][j] where i is the row index and j is the column index. l Keep in mind, of course, that both rows and columns are numbered starting from zero. For example, the statement: a[2][1] = 5; assigns 5 to the cell at row 3 column 2.

7 Declaration Using Initializer List l Similar to 1-D array, a two-dimensional array can be created as a list of array initializers, one for each row in the array, as shown by the following: int[][] a = { { 1, 0, 12, -1 }, { 7, -3, 2, 5 }, { -5, -2, 2, 9 } }; l To realize the above, the compiler has to do the following : int[][] a = new int[3][]; a[0] = new int[4]; a[0][0] = 1; a[0][1] = 0;... a[1] = new int[4]; a[1][0] = 7; a[1][1] = -3;......

8 Examples l The following example fills each cell in a 2-D array with the sum of its row and column indices. class FillTwoDArray { public static void main (String[] args) { int[][] a = new int[4][5]; for (int row=0; row < a.length; row++) for (int col=0; col < a[0].length; col++) a[row][col] = row+col; } l 2-D array can be easily extended to any dimension. For example, to declare a 3-Dimensional array, we need only add one more bracket as in: int [][][] threeDArray = new int [10][20][15]; l The next example reads two 2-D arrays, compute their sums and print the result.

9 Examples (Cont’d) import java.io.*; public class Matrix { static BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int row, column; System.out.print("Enter number of rows: "); row = Integer.parseInt(stdin.readLine()); System.out.print("Enter number of columns: "); column = Integer.parseInt(stdin.readLine()); double[][] a = createArray(row, column); double[][] b = createArray(row, column); double[][] c = sum2DArray(a, b); System.out.println("The sum of the two arrays is"); print2DArray(c); } static double[][] createArray(int row, int col) throws IOException { double[][] array = new double[row][col]; System.out.println("Enter elements for a "+row+" by "+col+" matrix"); for (int i=0; i<row; i++) for (int j=0; j<col; j++) array[i][j] = Double.parseDouble(stdin.readLine()); return array; }

10 Examples (Cont’d) static double[][] sum2DArray(double[][] a, double[][] b) { int row = a.length; int col = a[0].length; double[][] c = new double[row][col]; for (int i=0; i<row; i++) for (int j=0; j<col; j++) c[i][j] = a[i][j] + b[i][j]; return c; } static void print2DArray(double[][] a) { int row = a.length; int col = a[0].length; for (int i=0; i<row; i++) { for (int j=0; j<col; j++) System.out.print(a[i][j]+ "\t"); System.out.println(); }