You are on page 1of 15

Chapter 9

Working with Array a1nd String

Quick Chapter Summ~ry


Array
~Array is a variable representing a collection of
homogeneous type of elements.
tArrays are useful to represent vector, matrix and other
multi-dimensional data.
tvector Is a one dimensional (1-D) data structure that can
be used to store list of items like characters, numbers.
t Matrix is used to represent two dimensional (2-D) data
structure like table of rows and columns.
[TI
Creating an Array
t Creating an array Is a 2 step process :
(1) Declare an array object

(2) Create an array object

t An array object can be ueated In 2 ways :


(1) Using new operator and specifying the size

(2) Directly Initializing the concept of array

[TI
1-D array
t Array with single dimension is known as 1-D array.
t Example : Vector
t Instead of declaring individual variables like
markl, mark2, mark3, mark4, markS
we declare array mark[S]
t Index Position : mark[0],mark[1],mark[2],mark[3],mark[4]
tTo declare a 1-D array we use a pair of square brackets [ ].
t Syntax:<data type><array name>[ ]; Example : int marks[ ];
CT]
To declare 1-D array
int marks[ ] = new int [SJ;
- - ----
tHere,
marks= name of array.

Int data type uses 4 bytes storage space.

Thus array marks require 5 x 4 = 20 bytes In contiguous


location In memory.

[I]
Example : 1-D array
tThe array variable martls has an index I•~=i,t,A.\'-\ ltl;I
value from Oto 4.
. . Amy Amy
t marks[O] refers to the first element. ldatact C. Wti
t marks[4] refers to the last element.
t 1-D array is initialized using comma RMftct
VnNt
separated values of data element In
braces { }.
t Example : Int marks[ J = {90, 60, 70, 65, 80};
[TI
2-D array
t Array with two dimension Is known as 2-D array.
t 2-D arrays are used to store tabular data In the form of
rows and columns.
t Example : To store 5 student's marks in 3 tests, we may
use a tabular arrangement of marks 5 rows and 3 columns.
St•d~nts Tesll Testl Tes&J
I ~o 60 70
2 3S 30 so
3 70 1S 80
4 80 85 90
s 40 so 55

[TI
t In Java, 2-0 array can be declared using array name and
two pairs of square brackets [ ] [ ] to specify the size of
two dimensions row and column respectively.

int marks [ ] [ ] = new in [SJ [3];


t Here, the logical view of array element is a table of 5 rows
and 3 columns.
t So, 5 x 3 = 15 Integer values.
tint store 4 bytes, so 15 x 4 = 60 bytes memory store.
t Java does not support multi-dimensional arrays directly.
o=J
- -
tTo create 2-D array, we have to create an array of array.
t In marks [S] [3], it creates 1-D array object of 5 elements
and each element is 1-D array object of 3 integers.
intnuui..s =- new int (5)(3);

V,u ·fablt-

,f)(O) _..(4)(11 ---141(2)

Rderence
\ ·11ri11bles

[TI
Points to be remembered
...,,,,-it Array Is a collection of homogeneous type of data.
~ r a y elements can be accessed using Index for each
dl_menslon In [ ].

It Index value starts with O(zero).


It Attribute 'length' Is used to determine the size of the
dimension.
String_
t String Is nothing but a sequence of characters.
t String or Sequence of characters Is enclosed between
double quoteO
t In Java, characters are stored u s l n ~
tJava supports 2 types of strings: (1) String (2) StringBuffer
t Determine the length of string using length() method.
t Convert a string Into an array of bytes using getBytes()

00
String class methods
t String class provides methods for below tasks :
• Compare string
• Find length of string
• Combining strings
• Obtaining substrings
• Converting strings
• Splitting strings
• Searching for character or patterns In string
Date class
Date object using current system
(1) Date()
time
Number of milliseconds since 1,
(2) Long getTime()
Janua 1970 GMT
Date Specified time In milliseconds
(3) elapsed since 1, January 1970
(long elapsedTime) GMT (GMT : Greenwich Mean Time)
(4) String toString() Representing date and time
void setTime
(5) Sets new date and time
(long elapsedTime)
calendar class
Year of calendar
MONTH Month of calendar (O-Jan, 11-Dec)
DATE Day of calendar month.
(4) DAY_OF_MONTH Same as DATE.
(5) HOUR Hour in 12-hour notation
(6) HOUR_OF_DAY Hour in 24-hour notation
(7) MINUTE Minute
(8) SECOND Second
~
(9) AM_PM 0 for AM and 1 for PM
Day number within a week
Sun-1 ..... Sat-7
) 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 1st day
t Date class/ Cal_e ndar class provided In java.utll package.
t Date class encapsulate both date and time and represents
the value using milliseconds precision.
tCalendar store year, month, date, hour, minute, second.

You might also like