TOPICS TO COVER:-- Array declaration and use.

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

Chapter 9 Introduction to Arrays. Fundamentals of Java

&KDSWHU$UUD\VDQG9HFWRUV

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

Chapter 6: Arrays. Presentation slides for. Java Software Solutions. for AP* Computer Science 3rd Edition

11/19/2014. Arrays. Chapter 6: Arrays. Arrays. Arrays. Java Software Solutions for AP* Computer Science A 2nd Edition

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

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

ECE 122. Engineering Problem Solving with Java

Pace University. Fundamental Concepts of CS121 1

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

Activity 3: Data Types

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

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

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

CSC Java Programming, Fall Java Data Types and Control Constructs

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Introduction to Programming (Java) 4/12

Review of Important Topics in CS1600. Functions Arrays C-strings

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Lecture 04 FUNCTIONS AND ARRAYS

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

COMP 202. Programming With Arrays

Array. Prepared By - Rifat Shahriyar

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

Subject: Computer Science

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Accelerating Information Technology Innovation

Arrays and Basic Algorithms

H212 Introduction to Software Systems Honors

Arrays Chapter 7. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

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

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CS 112 Introduction to Programming

Excel Functions & Tables

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

STUDENT LESSON A12 Iterations

( &% class MyClass { }

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

C: How to Program. Week /Apr/16

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

HST 952. Computing for Biomedical Scientists Lecture 5

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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.

PROGRAMMING FUNDAMENTALS

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

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Full file at

Last Class. While loops Infinite loops Loop counters Iterations

Programming in OOP/C++

Introduction to the Java Basics: Control Flow Statements


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

Practice exam for CMSC131-04, Fall 2017

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

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

Loops. CSE 114, Computer Science 1 Stony Brook University

FORM 2 (Please put your name and form # on the scantron!!!!)

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSci 1113, Fall 2015 Lab Exercise 7 (Week 8): Arrays! Strings! Recursion! Oh my!

Excel Functions & Tables

Chapter 7: Arrays and the ArrayList Class

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Computer Programming C++ (wg) CCOs

Methods and Data (Savitch, Chapter 5)

Lesson 3: Accepting User Input and Using Different Methods for Output

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

ECE 122. Engineering Problem Solving with Java

Data Structure. Recitation IV

Introduction to Programming Using Java (98-388)

Types, lists & functions

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

AP Computer Science Homework Set 1 Fundamentals

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

4. Java language basics: Function. Minhaeng Lee

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

How to declare an array in C?

ARRAYS, RECURSION, AND COMPLEXITY

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Chapter 5 C Functions

JME Language Reference Manual

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

Variables and Functions. ROBOTC Software

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Unit 3 Decision making, Looping and Arrays

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 7. Arrays are objects that help us organize large amounts of information

INDIAN SCHOOL SOHAR FIRST TERM EXAM ( ) INFORMATICS PRACTICES

Transcription:

ARRAYS in JAVA

TOPICS TO COVER:-- Array declaration and use. One-Dimensional Arrays. Passing arrays and array elements as parameters Arrays of objects Searching an array Sorting elements in an array

ARRAYS An array is group of like-typed variables that are referred to by a common name. Each value has a numeric index The entire array has a single name 0 1 2 3 4 5 6 scores 50 12 45 78 66 100 125 An array can be of any type. An array of size N is indexed from zero to N-1 Specific element in an array is accessed by its index. Can have more than one dimension INTEGER FLOAT

2D Array Elements Requires two indices Row Which cell is CHART [3][2]? Column [0] [1] [2] [3] [4] [5] [0] 0 97 90 268 262 130 [1] 97 0 74 337 144 128 [2] 90 74 0 354 174 201 [3] 268 337 354 0 475 269 [4] 262 144 174 475 0 238 [5] 130 128 201 269 238 0 CHART

Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 45 (the 3rd value in the array) 0 1 2 3 4 5 6 5 50 12 45 78 66 100 125

DECLARAING ARRAYS The general form of 1-d array declaration is:- type var_name[ ]; 1. Even though an array variable scores is declared, but there int, float, char array name E.g.:--- int scores [ ]; is no array actually existing. 2. score is set to NULL, i.e. an array with NO VALUE. scores int[] scores = new int[10]; NULL To link with actual, physical array of integers.

Declaring Arrays Some examples of array declarations: int[ ] numbers; int numbers[]; double[] prices = new double[500]; boolean[] flags; flags = new boolean[20]; int[] numbers1, numbers2, numbers3; int numbers1[], numbers2, numbers3; This declares one reference variable to an integer array and two primitive integer variables 8

ARRAY INITIALIZATION The size of the array is determined by the number of items in the initializer list. An initializer list can only be used only in the array declaration Giving values into the array created is known as INITIALIZATION. The values are delimited by braces and separated by commas Examples: int[ ] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476}; char[ ] lettergrades = {'A', 'B', 'C', 'D', F'}; Note that when an initializer list is used: the new operator is not used no size value is specified

ACCESSING ARRAY A specific element in an array can be accessed by specifying its index within square brackets. All array indexes start at ZERO. Example:- System.out.println(units[4]); mean = (units[0] + units[1])/2; 147 + 323 /2 =235 0 1 2 3 4 5 6 7 8 9 int[ ] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476};

PROCESSING ARRAY ELEMENTS Often a for( ) loop is used to process each of the elements of the array in turn. The loop control variable, i, is used as the index to access array components EXAMPLE:- int i; for(i=0;i<=2;i++) { score [0] System.out.println(+score[i]); } 0 1 2 3 4 5 6 score 50 12 45 78 66 100 125

int i; for(i=1;i<=2;i++) { score [1] System.out.println(+score[i]); } 0 1 2 3 4 5 6 score 50 12 45 78 66 100 125

Bounds Checking Once an array is created, it has a fixed size An index used in an array reference must specify a valid element That is, the index value must be in bounds (0 to N-1) The Java interpreter throws an ArrayIndexOutOfBoundsException if an array index is out of bounds This is called automatic bounds checking 14

Bounds Checking For example, if the array score can hold 100 values, it can be indexed using only the numbers 0 to 99 If i has the value 100, then the following reference will cause an exception to be thrown: 15 System.out.println (score[i]); It s common to introduce off-by-one errors when using arrays for (int i=0; i <= 100; i++) score[i] = i*50; problem

ARRAY OF OBJECTS Create a class student containing data members Name, Roll_no, and Marks.WAP in JAVA to accept details of 5 students. Print names of all those students who scored greater than 85 marks student student[0] student[1] student[2] student[3] Chris,101,85 Brad, 102,75.8 Andrew, 103,75.9

Recursion Recursion is the process of defining something in terms of itself. It allows a method to call itself. compute() In general, to solve a problem using recursion, you break it into sub problems. AREAS WHERE RECURSION CAN BE USED. FACTORIAL OF A NUMBER. FIBONACCI SERIES. GCD OF TWO NUMBERS. TOWER OF HANOI. QUICK SORT. MERGE SORT.

TWO- DIMENSIONAL ARRAY DECLARATION:- Follow the same steps as that of simple arrays. Example:- int [ ][ ]; chart = new int [3][2]; int chart[ ][ ] = new int [3][2]; INITIALIZATION:- int chart[3][2] = { 15,16,17,18,19,20}; 15 16 17 18 19 20 int chart[ ][ ] = { {15,16,17},{18,19,20} }; 15 16 17 18 19 20

PROGRAM FOR PRACTISE The daily maximum temperature is recorded in 5 cities during 3 days. Write a program to read the table elements into 2-d array temperature and find the city and day corresponding to Highest Temperature and Lowest Temperature. Write a program to create an array of objects of a class student. The class should have field members id, total marks and marks in 3 subjects viz. Physics, Chemistry & Maths. Accept information of 3 students and display them in tabular form in descending order of the total marks obtained by the student.