2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Arrays Chapter 6 Chapter 6.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
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 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Multidimensional Arrays in Java Vidhu S. Kapadia.
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.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
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.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
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.
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.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Arrays. An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
August 6, 2009 Data Types, Variables, and Arrays.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Title Slid CSC 444 Java Programming Arrays By Ralph B. Bisland, Jr.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
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.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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 Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
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.
Array and String.
CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data.
ARRAYS Multidimensional realities Image courtesy of
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
Arrays Declare the Array of 100 elements Notes
Nested Loop Review and Two-Dimensional Arrays
Introduction To Programming Information Technology , 1’st Semester
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Presentation transcript:

2D-Arrays Quratulain

Learning Objectives Two-dimensional arrays Declaration Initialization Applications

Introduction Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach. when created, arrays are automatically initialized with the default value of their type – String[] s = new String[100]; // default values: null – boolean[] b = new boolean[4]; // default values: false – int[] [] i = new int[10][10]; // default values: 0

Sorted array element int i, j, index, tmp; int n = arr.length; for (i = 0; i < n - 1; i++) { index = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[index]) index = j; if (index != i) { tmp = arr[i]; arr[i] = arr[index]; arr[index] = tmp; }

Introduction The first dimension represents the rows, the second dimension, the columns curly braces {} may also be used to initialize two dimensional arrays. Again they are only valid in array declaration statements. – int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} }; – you can initialize the row dimension without initializing the columns but not vice versa int[][] myArray = new int[5][]; – int[][] myArray = new int[][5]; // illegal Now initialize columns in each row – myArray[0] = new int[3]; – myArray[1] = new int[4]; – myArray[2] = new int[5];

Demonstrate a two-dimensional array // Demonstrate a two-dimensional array. class TwoD { public static void main(String args[]) { int t, i; int table[][] = new int[3][4]; for(t=0; t < 3; ++t) { for(i=0; i < 4; ++i) { table[t][i] = (t*4)+i+1; System.out.print(table[t][i] + " "); } System.out.println(); }

Home work (Magic Square) A magic square of order n is an arrangement of n 2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. Develop code using 4-rules discussed in class. Here sum is

Ragged Array The use of irregular (or ragged) multidimensional arrays is not recommended for most applications, because it runs contrary to what people expect to find when a multidimensional array is encountered. However, irregular arrays can be used effectively in some situations. – For example, if you need a very large two-dimensional array that is sparsely populated (that is, one in which not all of the elements will be used), an irregular array might be a perfect solution.

Example // Manually allocate differing size second dimensions. class Ragged { public static void main(String args[]) { int riders[][] = new int[7][]; riders[0] = new int[10]; riders[1] = new int[10]; riders[2] = new int[10]; riders[3] = new int[10]; riders[4] = new int[10]; riders[5] = new int[2]; riders[6] = new int[2]; int i, j; // fabricate some fake data for(i=0; i < 5; i++) for(j=0; j < 10; j++) riders[i][j] = i + j + 10; for(i=5; i < 7; i++) for(j=0; j < 2; j++) riders[i][j] = i + j + 10; System.out.println("Riders per trip during the week:"); for(i=0; i < 5; i++) { for(j=0; j < 10; j++) System.out.print(riders[i][j] + " "); System.out.println(); } System.out.println(); System.out.println("Riders per trip on the weekend:"); for(i=5; i < 7; i++) { for(j=0; j < 2; j++) System.out.print(riders[i][j] + " "); System.out.println(); }

Assign Array Reference when you assign one array reference variable to another you are not causing a copy of the array to be made, nor are you causing the contents of one array to be copied to the other. int nums1[] = new int[10]; int nums2[] = new int[10]; for(i=0; i < 10; i++) nums1[i] = i; for(i=0; i < 10; i++) nums2[i] = -i; nums2 = nums1; // now nums2 refers to nums1