Multidimensional Arrays in Java Vidhu S. Kapadia.

Slides:



Advertisements
Similar presentations
1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
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.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Chapter 9 Introduction to Arrays
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
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.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
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.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Java SE 8 for Programmers, Third Edition
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Static arrays.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
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.
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.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Two-Dimensional Arrays
Arrays in Java.
Presentation transcript:

Multidimensional Arrays in Java Vidhu S. Kapadia

The Basic Definitions What is an Array? An array is a fixed size sequential collection of elements of identical types. They are created with either a new operator or array initializes The storage space for arrays is allocated from the garbage- collected heap. The element in an array are indexed by the integers 0 to n- 1, where n is the size of the array

Declaring an Array Using the new operator  new Type [ ] Here the size of the array must be specified, and all the elements in the array are initialized to the default initial values based on the types of elements.  int ia1 = new int [3]; //creates an integer array of size 3; all elements are initialized to 0  Point pa [ ] = new Point [10]; //creates an array of points of size 10; all elements are initialized to null

Declaring an Array Using the one-dimensional array initializer  { v 0, v 1, ….., v n-1 } The values for v 0, v 1, ….., v n-1 are the initial values of the elements in the array. The size of the array is determined by the number of initial values provided in the array initializer  Int ia2 [ ] = {1, 2, 3}; // Creates an array of size 3; elements are initialized to 1, 2, and 3 respectively

What are Multidimensional Arrays A multidimensional array is treated as an array of arrays. –Let a be a k-dimensional array; the elements of a can be accessed using the following syntax: a [ i 1 ] [ i 2 ]…[ i k ] where n 1, n 2, ….., n k

Multidimensional Arrays Applying what we learned about simple arrays to multidimensional array, we get the following: A k-dimensional array can be created with either of the following methods: –Using the new operator –Using the k-dimensional initializer

Creating k-dimensional Array Using the new operator new Type [ n1] [n2]…[nk] Size of each dimension is n1, n2, …,nk, respectively. All the elements in the array are initialized to default initial values based on the type of the elements Using k-dimensional array initializer { I 1, I 2,...., I k } where each I 1, I 2,...., I k is a (k-1)- dimensional array initializer.

Examples… Using the new operator, the following example is of creating and initializing a two-dimensional array. double mat1[ ][ ] = new double[4][5]; This creates a 4 x 5 two-dimensional array. All elements are initialized to 0.0. Using k-dimensional array initializer, the following example is of creating and initializing a two-dimensional array. int mat2[ ] [ ] = {{1, 2, 3}, {4, 5, 6}}; This creates a 2 x 3 two- dimensional array. The elements are initialized as: mat2[0][0]=1 mat2[1][0]= 4 mat2[0][1]=2 mat2[1][1]= 5 mat2[0][2]=3 mat2[1][2]= 6

Illustrating Two-Dimensional Array

Processing Multidimensional Arrays Multidimensional arrays are often processed using for statements. To process all the items in a two-dimensional array, a for statement needs to be nested inside another. int[ ][ ] A = new int[3][4]; The following loop stores 0 into each location in two dimensional array A : for (int row = 0; row < 3; row++) { for (int column = 0; column < 4; column++) { A [row][column] = 0; }

An Example: Using a mathematical function in the Java Math class and handling of two–dimensional arrays, the following example calculates the max – min value.

public class MaxMin{ public static void main(String[]args){ double mat[ ] [ ]= { {2.3, 5.1, 9.9}, {8.3, 4.5, 7.7}, { 5.2, 6.1, 2.8}} int n = mat.length; int m= mat[0].length; double maxmin = 0.0; for (int j = 0; j < m; j++){ double min = mat[j][0]; for (int i = 1; I <n ; i ++){ min = Math.min(min,mat[i][j]); } if (j==0){ maxmin = min; }else{ maxmin = Math.max(maxmin, min); } System.out.println( “ The max-min value is ” + maxmin); } Running the program produces the following result: The max-min value is 4.5

Keep in mind that… Computer memory is linear – In accessing a multidimensional array you are really accessing a one-dimensional array in memory. You can simulate multidimensional arrays by packing them into a one-dimensional array. The two manners of packing the one-dimensional array are row-major order, where the array is filled one row at a time; and column-major order, where columns are placed into the array.

Interesting Tidbits about Arrays in Java One of the main differences between the arrays in Java and those in C/C++ is that the Java arrays are ALWAYS bounds-checked at run time. Array bound-checking will automatically detect a major source of faults in programs, that is exceeding the bounds of arrays while accessing or manipulating their elements. There is no limit on the number of dimensions that an array type can have. However, arrays of dimension three or higher are fairly uncommon.

Jia, Xiaoping. Object Oriented Software Development using Java- 2 nd edition, Adison Wesley 2003 The historical collection classes – Arrays, IBM. April, 16, 2006, arrays/ Array initialization, The Code Guru. April, 16, 2006, References