Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Similar documents
Chapter 9 Introduction to Arrays. Fundamentals of Java

Object Oriented Programming. Java-Lecture 6 - Arrays

Lesson 05: How to Insert, Update, and Delete Data. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Instructor: Eng.Omar Al-Nahal

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Lesson 09 Working with. SDI and MDI. MIT 31043: Rapid Application Development By: S. Sabraz Nawaz Senior Lecturer in MIT Dept. of MIT, FMC, SEUSL

Chapter 12 Two-Dimensional Arrays

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

More non-primitive types Lesson 06

ECE 122. Engineering Problem Solving with Java

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Chapter 7 Array. Array. C++, How to Program

Two Dimensional Arrays

TOPICS TO COVER:-- Array declaration and use.

Introduction to Programming Using Java (98-388)

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

H212 Introduction to Software Systems Honors

Chapter 6: Using Arrays

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Array. Prepared By - Rifat Shahriyar

Arrays. Eng. Mohammed Abdualal

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Arrays. Theoretical Part. Contents. Keywords. Programming with Java module 3

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Database Programming with PL/SQL

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Arrays (Deitel chapter 7)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

MIT Database Management Systems

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java Programming Training for Experienced Programmers (5 Days)

MIT Database Management Systems Lesson 03: ER-to-Relational Mapping

Computer Science is...

CS159. Nathan Sprague. September 11, 2015

Spring 2010 Java Programming

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Introduction to the Java Basics: Control Flow Statements

CS111: PROGRAMMING LANGUAGE II

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 170 Java Programming 1. Week 10: Loops and Arrays

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Flow Control. CSC215 Lecture

JME Language Reference Manual

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Arrays and Collections. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Last Class. More on loops break continue A bit on arrays

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

The Warhol Language Reference Manual

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

MIT Database Management Systems Lesson 03: Entity Relationship Diagrams

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

CS313D: ADVANCED PROGRAMMING LANGUAGE

APCS Semester #1 Final Exam Practice Problems

Programming Control Structures with JavaScript (Part 2) MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

Control Statements. Musa M. Ameen Computer Engineering Dept.

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

DINO. Language Reference Manual. Author: Manu Jain

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Procedural Programming

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

AP Computer Science Lists The Array type

Lecture 04 FUNCTIONS AND ARRAYS

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

C# and Java. C# and Java are both modern object-oriented languages

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Decision Making in C

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Chapter 4: Control structures. Repetition

BITG 1113: Array (Part 1) LECTURE 8

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Lexical Considerations

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

CSC Java Programming, Fall Java Data Types and Control Constructs

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

HST 952. Computing for Biomedical Scientists Lecture 5

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

Transcription:

Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Array An array is a group of variables (called elements or components) containing values that all have the same type Index (or subscript) of the element in array c Name of the array: c c[ 0 ] 5 c[ 1 ] 55 c[ 2 ] -60 c[ 3 ] 84 c[ 4 ] 73 c[ 5 ] 2565 c[ 6 ] 0 c[ 7 ] 88 c[ 8 ] 7 c[ 9 ] 66 Slide 2

Declaring and Creating Arrays Array objects occupy space in memory. Like other objects, arrays are created with keyword new To create an array object, you specify the type of the array elements and the number of elements as part of an array-creation expression that uses keyword new The declaration and initialization can also be done as shown below Slide 3

Assigning Values to Array To refer to the elements, you use an index that ranges from zero (the first element in the array) to one less than the total number of elements If you specify an index that s less than zero or greater than the upper bound of the array, an ArrayIndexOutOfBoundsException will be thrown Slide 4

Accessing the Array s Values Slide 5

Array Initializer You can instantiate an array and provide initial values with an array initializer a comma-separated list of expressions (called an initializer list) enclosed in braces The number of values determines the size of the array type[] arrayname = { value1, value2, value3,... }; Slide 6

Getting Array s Size Slide 7

Using Loops With Array Slide 8

So An array can store more than one primitive type or object An element is one of the items in an array To create an array, you must declare a variable of the correct type and instantiate an array object that the variable refers to To declare an array variable, you code a set of empty brackets after the type or the variable name To instantiate an array, you use the new keyword and specify the length, or size, of the array in brackets following the array type You can specify the array length by coding a literal value or by using a constant or variable of type int When you instantiate an array of primitive types, numeric types are set to zeros and boolean types to false When you create an array of objects, they are set to nulls Slide 9

Exercise 01 Create an array of double data type to hold the list of 10 prices Use the for loop to display the values on screen Modify your code to display the sum of all the prices Slide 10

Exercise 02 Create a String array to hold the values of names of players in a cricket team, use Array Initializer for this purpose Display the names of the players using for loop Slide 11

Exercise 03 - Answer Slide 13

Enhanced for Statement The enhanced for statement iterates through the elements of an array without using a counter, thus avoiding the possibility of stepping outside the array The syntax o parameter: has a type and an identifier (e.g., int number) o arrayname is the array through which to iterate The type of the parameter must be consistent with the type of the elements in the array Slide 14

Example 01 Slide 15

Two Dimensional Arrays Slide 16

Two Dimensional Arrays Arrays that require two indices to identify a particular element are called two-dimensional arrays (Multidimensional arrays can have more than two dimensions.) Use two indexes and allow data to be stored in a table that consists of rows and columns In general, an array with m rows and n columns is called an m-by-n array Slide 17

Two-dimensional array with three rows and four columns Slide 18

Creating a rectangular array The syntax for creating a rectangular array type[][] arrayname = new type[rows][columns]; A statement that creates a 3x2 array int[][] numbers = new int[3][2]; Slide 19

How to assign values to a rectangular array The syntax for referring to an element of a rectangular array arrayname[rowindex][columnindex] The indexes for a 3x2 array [0][0] [0][1] [1][0] [1][1] [2][0] [2][1] Slide 20

How to assign values to a rectangular array Code that assigns values to the array numbers[0][0] = 1; numbers[0][1] = 2; numbers[1][0] = 3; numbers[1][1] = 4; numbers[2][0] = 5; numbers[2][1] = 6; Slide 21

Array Initializer Code that creates a 3x2 array and initializes it in one statement int[][] numbers = { {1,2}, {3,4}, {5,6} }; Slide 22

Code that processes a rectangular array with nested for loops Slide 23

End of Lecture