Upload
mishallmujahid
View
218
Download
0
Embed Size (px)
Citation preview
8/9/2019 Java10 Collections
1/29
JAVA Software Development
Paradigm
Advance OO Programming
BSCS Semester 6
MCS 3
Course nstructor! Ms" ram
8/9/2019 Java10 Collections
2/29
#ava"util"$andom% Benc&mar's uses t&e #ava"util"$andom class
( a more controlled wa) to generate random
num*ers"
% f we set t&e same seed+ we get t&e same,random- se.uence"
$andom generator/ 0 new $andom12
$andom generator4 0 new $andom1seed2 long seed;
t&e seed is different
eac& time
int ' 0 generator"ne5tnt 1n2
dou*le 5 0 generator"ne5tDou*le 12
≤ k 7 n
≤ x 7 1
8/9/2019 Java10 Collections
3/29
/383
#ava"util"Arra)s
% Provides static met&ods for dealing wit&arra)s"
% 9or's for arra)s of num*ers+ Strings+ and
O*#ects"
% Met&ods!
int pos 0 Arra)s"binarySearch 1arr+ target2
Arra)s"sort 1arr2
Arra)s"fill 1arr+ value2 :: fills arr wit& a given value
String str 0 Arra)s"toString1arr2
Arra)s"asList1arr2$eturns a representation of
arr as a fi5ed8lengt& list
8/9/2019 Java10 Collections
4/29
/38;
#ava"util"Collections
% Provides static met&ods for dealing wit& Arra)
8/9/2019 Java10 Collections
5/29
/=8>
Collection+ Iterator
% Collection interface represents an) collection"
% An iterator is an o*#ect t&at &elps to traverset&e collection 1process all its elements in
se.uence2"
% A collection supplies its own iterator1s2+1returned *) collection?s iterator met&od2 t&e
traversal se.uence depends on t&e
collection"
@interface
Collection
@interface
Iterator
8/9/2019 Java10 Collections
6/29
/=86
Collections
% Collection+ Iterator
%
8/9/2019 Java10 Collections
7/29/=8F
Overview
8/9/2019 Java10 Collections
8/29/=8G
Collection
Met&ods *oolean isHmpt) 12
int siIe 12
*oolean contains 1O*#ect o*#2 *oolean add 1H o*#2
*oolean remove 1H o*#2
terator7H iterator 12
:: """ ot&er met&ods
Supplies an
iterator for t&is
collection
@interface
Collection
@interface
Iterator
8/9/2019 Java10 Collections
9/29/=8=
Iterator
Met&ods
*oolean &asEe5t 12 H ne5t 12
void remove 12
@interface
Collection
@interface
Iterator
9&at?s ,ne5t- isdetermined *) a
particular collection
$emoves t&e last
visited element
8/9/2019 Java10 Collections
10/29/=8/
8/9/2019 Java10 Collections
11/29/=8//
List 7E
Met&ods
@interface
Collection
@interface
Iterator
@interface
ListIterator
@interface
List
:: All Collection7E met&ods+ plus!
H get 1int i2 H set 1int i+ H o*#2
void add 1int i+ H o*#2
H remove 1int i2
int inde5Of 1O*#ect o*#2
8/9/2019 Java10 Collections
12/29/=8/4
ListIterator 7E
Met&ods
@interface
Collection
@interface
Iterator
@interface
ListIterator
@interface
List
:: &e t&ree Iterator 7E met&ods+ plus! int ne5tnde5 12
*oolean &asPrevious 12
H previous 12
int previousnde5 12
void add 1H o*#2
void set 1H o*#2
Can traverse t&e
list *ac'ward
Can add elements to t&e list 1inserts
after t&e last visited element2
Can c&ange elements 1c&anges
t&e last visited element2
8/9/2019 Java10 Collections
13/29/=8/3
Arra)
8/9/2019 Java10 Collections
14/29/=8/;
8/9/2019 Java10 Collections
15/29
/=8/>
8/9/2019 Java10 Collections
16/29
/=8/6
Arra)
8/9/2019 Java10 Collections
17/29
/48/=
#ava"util"Arra)
8/9/2019 Java10 Collections
18/29
/484
Arra)+ Arra)
8/9/2019 Java10 Collections
19/29
/484/
Arra)
8/9/2019 Java10 Collections
20/29
/4844
Arra)
8/9/2019 Java10 Collections
21/29
/4843
Arra)
8/9/2019 Java10 Collections
22/29
/484;
Arra)
8/9/2019 Java10 Collections
23/29
/484>
Arra)
8/9/2019 Java10 Collections
24/29
/4846
Arra)
8/9/2019 Java10 Collections
25/29
/484F
Arra)
8/9/2019 Java10 Collections
26/29
/484G
,Kor Hac&-
% 9or's *ot& wit& standard arra)s and Arra)
8/9/2019 Java10 Collections
27/29
,Kor Hac&-
8/9/2019 Java10 Collections
28/29
/483
,Kor Hac&-
8/9/2019 Java10 Collections
29/29
/= 3/
terator VS ,Kor Hac&-