Lesson:9 Working with Array and String

Similar documents
More non-primitive types Lesson 06

Lab 14 & 15: String Handling

Abstract Classes Interfaces


We now start exploring some key elements of the Java programming language and ways of performing I/O

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

More on variables and methods

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

COMPUTER APPLICATIONS

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

A variable is a name for a location in memory A variable must be declared

STUDENT LESSON A10 The String Class

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Computational Expression

Chapter 9 Abstract Classes and Interfaces

Class Library java.lang Package. Bok, Jong Soon

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

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

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Class Library java.util Package. Bok, Jong Soon

Object-Oriented Programming

String. Other languages that implement strings as character arrays

Array. Prepared By - Rifat Shahriyar

Date & Time Handling In JAVA

"Hello" " This " + "is String " + "concatenation"

Introduction to Programming Using Java (98-388)

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Programming for Engineers Arrays

Contents. Osp::Base::Object Osp::Base::String Osp::Base::Buffer Osp::Base::DateTime

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

EECS1710. Announcements. Labtests have been returned Term Test #01 marking is in progress. Next Labtest: Thu Oct 23/Fri Oct 24

Lecture Notes K.Yellaswamy Assistant Professor K L University

Utilities (Part 2) Implementing static features

The Year argument can be one to four digits between 1 and Month is a number representing the month of the year between 1 and 12.

appreciate the difference between a char and a string understand and use the String class methods

Building Strings and Exploring String Class:

Strings! Today! CSE String Methods!

Datatypes, Variables, and Operations

Getting started with Java

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

USING LIBRARY CLASSES

ENGLISH Page 1 of 6. EXAM IN COURSE TDT4100 Object-Oriented Programming / IT1104 Programming, Advanced Course. Tuesday 29. Mai

Chapter 9 Lab Text Processing and Wrapper Classes

Creating Strings. String Length

CMSC 341. Nilanjan Banerjee

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Eng. Mohammed Abdualal

Class. Chapter 6: Data Abstraction. Example. Class

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Lab 3: Call to Order CSCI 2101 Fall 2018

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

String related classes

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

CMSC131. Arrays: The Concept

CHAPTER 6 MOST COMMONLY USED LIBRARIES

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.


Instructor: Eng.Omar Al-Nahal

The Warhol Language Reference Manual

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Designing data types. Overview. Object Oriented Programming. Alan Kay. Object Oriented Programming (OOP) Data encapsulation. Checking for equality

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Java Classes & Primitive Types

Lab 3: Call to Order CSCI 2101 Fall 2017

TOPICS TO COVER:-- Array declaration and use.

Java Classes & Primitive Types

Designing data types. Fundamentals of Computer Science I

Final Exam Practice. Partial credit will be awarded.

TEXT-BASED APPLICATIONS

Index COPYRIGHTED MATERIAL

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Motivations. Chapter 13 Abstract Classes and Interfaces

Chapter 6: Using Arrays

COS 226 Algorithms and Data Structures Week 3: Comparators, & Sorting (Video 5.D & Algorithms 2.1 and 2.2)

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

CST242 Strings and Characters Page 1

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Arrays. Eng. Mohammed Abdualal

Student Performance Q&A:

array Indexed same type

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

ADVANCED PROGRAMMING CONCEPTS

Inheritance, Polymorphism, and Interfaces

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

5/23/2015. Core Java Syllabus. VikRam ShaRma

Instructor s Notes Java - Beginning Built-In Classes. Java - Beginning Built-In Classes

EnableBasic. The Enable Basic language. Modified by Admin on Sep 13, Parent page: Scripting Languages

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

Chapter 8 Objects and Classes Part 1

TeenCoder : Java Programming (ISBN )

Lecture 2 Tao Wang 1

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

Transcription:

Introduction to Array: Lesson:9 Working with Array and String An Array is a variable representing a collection of homogeneous type of elements. Arrays are useful to represent vector, matrix and other multi-dimensional data. Vector is a one dimensional (1-D) data structure can be used to store list of items like characters, numbers. Matrix is used to represent two dimensional (2-D) data structure like table of rows and columns. Arrays are useful when same operations are to be performed on various elements of the similar type. All the elements of an array are stored in memory using contiguous storage space. Each element is identified by an index position associated with array variable. In Java, array is an object used to manage list of items. Creating an array is a two step process. 1. Declare an array object 2. Create an array object An array can be created in two ways: 1. Using new operator and specifying the size 2. Directly initializing the content of array 1-D array: Array with single dimension is known as 1-D array. For example, vector which can be seen as a collection of one or more scalar variables. Instead of declaring individual variables like marks1, marks2, marks3, marks4, marks5; we can declare array marks [5] and accesses individual variable elements as marks [0], marks [1], marks [2], marks [3], marks [4]. To declare a 1-D array we use a pair of square brackets [ ] after array name or after data type. Syntax: <data type><array name> [ ]; OR <data type> [ ] <array name; Example: int marks[ ]; // declare array object marks=new int[5]; // cerate array object int marks[]=new int[5]; OR int[] marks =new int[5]; Here name of the array is marks. It refers to memory location where five integer values are stored. Integer of int data type uses 4 bytes storage space. Thus, array marks requires 5*4 =20 bytes in contiguous location in memory. Int marks[]={90,60,70,65,80}; Marks Variable Element Array Content Array Index Marks[0] 90 0 Marks[1] 60 1 Marks[2] 70 2 Marks[3] 65 3 Marks[4] 80 4 Every element of an array is an individual variable that can be referred by using index. Like variables, to change the value of an element, we can use assignment statement referring a variable element on left side. We can perform various operations on array. For example, compare two arrays, copy all the elements of one array to another, and search for a specified element in array, sort element of array and so on. For all such operations, we can use procedure otherwise, we can use various static methods provided by java using java.util.arrays class. In java, Array is treated as an object of this Arrays class. Std.12EM L-9 Working with Array and String (www.verymicro.org) 1

To search an element of array, arrays class provides binarysearch() method. 2-D Array: Two dimensional (2-D) arrays are used to store tabular data in the form of rows and columns. For example, to store 5 student s marks in 3 tests, we may use a tabular arrangement of marks in 5 rows and 3 columns. In java 2-D array can be declare using name and two pairs of square brackets [] [] to specify the size of two dimensions row and column respectively. Example: int marks[][] = new int [5][3]; Students Test1 Test2 Test3 1 50 60 70 2 35 30 50 3 70 75 80 4 80 85 90 5 40 50 55 Here, the logical view of array element is 5 rows and 3 columns. Physically they are stored in memory using contiguous memory locations for 15 integers that is 60 bytes. Java does not support multi-dimensional array directly. To create 2-D array, we have to create an array of array. There is no limit on number of dimensions. Std.12EM L-9 Working with Array and String (www.verymicro.org) 2

Int Marks = new int [5][3] Marks Variable Marks[0] Marks[0][0] Marks[0][1] Marks[0][2] Marks[1] Marks[1][0] Marks[1][1] Marks[1][2] Marks[2] Marks[2][0] Marks[2][1] Marks[2][2] Marks[3] Marks[3][0] Marks[3][1] Marks[3][2] Marks[4] Marks[4][0] Marks[4][1] Marks[4][2] Declaration and initialization of 2-D array is very similar to 1-D array except the number of dimensions. In 2-D array, each row is considered as 1-D array element. Points to be remembered: Array is a collection of homogeneous type of data. Array elements can be accessed using index for each dimension in [] Index value start with zero. Multi-dimensional arrays can have variable size of 2 nd, 3 rd.. Dimensions. Attribute length is used to determine the size of the dimension. Array is treated as an object. Declaring an array without initialization does not create an array object. Std.12EM L-9 Working with Array and String (www.verymicro.org) 3

Strings: String is a nothing but a sequence of character. So, 1-D array of characters can be considered as a string. To use variables that can store strings, Java supports two types of strings that are handled by two classes namely String and StringBuffer. Some of the constructors that can be used to create an object are as follows: String () without arguments create a String object with no character. String (char ary[]) creates a String object with its initial value using ary argument. String (char ary[], int start, int len) creates a String object using 1-D ary argument starting at ary[start] with len number of characters. String (String strobj) creates a String object which is same as, object specified in argument. String (String literal) creates a String object that refer to the literal specified in argument. String Class s: boolean equals(string str) boolean equalsignorecase(string str) int compareto(string str) int comparetoignorecase (String str) String class provides methods for other tasks as follow 1. Extracting part of string 2. Replacing character of substring 3. Splitting string into substring 4. Getting number of character 5. Getting character at specified index position 6. Converting string into an array of bytes 7. Converting string into lowercase or uppercase 8. Appending string 9. Copy string or part of string int length() char indexat(int index) byte[] getbytes() void getchars(int fromindex, int toindex,char target[], int targetindex) String concat(string str) String ToLowerCase() String ToUpperCase() Return true if invoking String is same as str Return true if invoking String is same as str ignoring case (case sensitive) Returns 0,>0,<0 integer if invoking string is equal to, greater thanor less than str respectively. Same as compareto but case sensitive Returns number of characters in invoking string Returns character at index position from the invoking string, index considered from 0 Returns an array of characters as bytes from invoking string Copies characters of invoking string from fromindex to toindex, 1 to target array from targetindex onwards. Returns a string after appending str with the invoking string Returns a string with all characters of invoking string converted to lowercase. Returns a string with all characters of invoking string converted to Uppercase. Std.12EM L-9 Working with Array and String (www.verymicro.org) 4

Date class: Java library also provides Date class in java.util package. Date class encapsulate both date and time and represent the value using milliseconds precision. Date() Construct Date objects using current system time. Date(long elapsedtime) Construct date object using specified time in milliseconds elapsed since January 1,1970 GMT String tostring() Returns a string representing date and time of invoking object Long gettime() Returns number of milliseconds since January 1,1970 GMT Void settime(long elapsedtime) Sets new date and time of an object using elapsed time. Calendar class: Like Date class, Calendar class is also provided in java.util package. This class can be used to extract detailed calendar information like year, month, date, hour, minute and second. Here, we will see the use of GregorianCalander subclass of calendar class. Constant YEAR Years of calendar MONTH Month of calendar (0 for January, 11 for December) DATE Day of calendar month DAY_OF_MONTH Same as DATE HOUR Hour in 12-hour notation HOUR_OF_DAY Hour in 24-hour notation MINUTE Minute SECOND Second AM_PM 0 for AM, 1 for PM DAY_OF_WEEK Day number within a week (1 for Sunday, 7 for Saturday) WEEK_OF_MONTH Week number within the month WEEK_OF_YEAR Week number within the year DAY_OF_YEAR Day number in the year (1 for the first day) Std.12EM L-9 Working with Array and String (www.verymicro.org) 5