0% found this document useful (0 votes)
38 views34 pages

Chap 8

This document discusses arrays in Java programming. It begins by defining an array as a collection of similar data types stored in contiguous memory locations and accessed via an index. The document then provides examples of declaring and initializing different types of arrays, such as int[], char[], and double[]. It explains that arrays must be initialized with the new keyword, passing the size of the array. Overall, the document provides a concise overview of arrays in Java, including their definition, declaration syntax, and initialization.

Uploaded by

jarinmail4490
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
38 views34 pages

Chap 8

This document discusses arrays in Java programming. It begins by defining an array as a collection of similar data types stored in contiguous memory locations and accessed via an index. The document then provides examples of declaring and initializing different types of arrays, such as int[], char[], and double[]. It explains that arrays must be initialized with the new keyword, passing the size of the array. Overall, the document provides a concise overview of arrays in Java, including their definition, declaration syntax, and initialization.

Uploaded by

jarinmail4490
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34



 8
+"#
##*') 


"78/4'9"+ "%8 :;"1'(<""='1'(9I2/($EC/6C2/0M#I"A5-78"l'" <#I"A5#'&


/LA+'$1'(:(1')<#&(2'6/($E4#'$0ADA  <""='0L/5 %B1%$B @C2/61,+/($E /LA+'$
@'040'$C/6/##K1I,B"<""='/A"D/(EJ&<##'&DH'6F %B/$MH9" XNO<#
1'("='G:9I26'"


8 +
/($E9">'?'.'@' 7/D,@<:( %B:;"I"A5C2/0M#<++/2'6/A6 %B&'0'(*9I21K+C2/0M#
I"A55%$@1,"G524#'$H'  D,@<:(/($E.:;"D,@<:( 0%B %I7B/5%$@1,"<DH.0%.='"@"&0'IA1G52
4#'$D,@ !5$D='<4"H69"4"H@$@'0.=' %B1K+C2/0M#4#H'"%8./$MHDA51," D,@/$H'6IH" 4'1
D2/61'(1='4"5D,@<:(9420I% "A5C2/0M#:;" int -7B/1K+C2/0M#G52 tH'!5$D,68 I7B/D,@<:(:;"
x1, x2, x3, x4<#x55,6"%8
 int x1, x2, x3, x4, x5;
D,@<:(5,61#H'@.*7/@H':;"D,@<:("#D,@1," !5$0%I7B/DH'61,"<#G0H0% @'0
&,0-,"LERSB61,"<#1," <DH4'1:(1')942D,@<:( x :;"D,@<:(/($E %B0.% ='"@"&0'IA1 t D,@
!5$9I2='&,B65,6"%8
 int []x = new int[5]; 
. ='942D,@<:(/($Ex:;"D,@<:( 0%B %I"A5C2/0M#:;"int RSB60%&0'IA1t D,@!5$0%
40'$#C&0'IA1D,86<DH m *S6 u <#:;"I"A5C2/0M#<++/2'6/A6 1#H'@7/H'C/6 x .1K+
D='<4"H6C/64"H@$@'0.='-7B//2'6/A6*S6D='<4"H6 %B 1K+C2/0M#&0'IA1C/6/($E<DH#D,@ 5,6
<&569"(M: %BzQP

  4! 1'(1K+D='<4"H6/2'6/A6C/6D,@<:(/($E


    
    


>'?'.'@'<+H6D,@<:(/($E:;"&/6:(> 7/
 /($EC/6C2/0M#I"A5-78"l'"
 /($EC/6C2/0M#I"A5#'&
/($EC/6C2/0M#I"A5-78"l'" 7//($E %B&'0'(*9I21K+C2/0M# 0%B %I"A5C2/0M#<++
-78"l'"I"A595I"A54"SB6G524#'$H'IH" /($EC/6C2/0M#I"A5 int 4(7//($EC/6C2/0M#I"A5
boolean:;"D2"

/($EC/6C2/0M#I"A5#'& 7//($E &%B '0'(*9I21K+C2/0M# %B :;"//:.KC/6#'&


95FG524#'$//:.KIH"/($EC/6C2/0M#I"A5String:;"D2"


8 + #)0>


>'?'.'@'&'0'(* %B.&(2'6/($E -7B/9I21K+C2/0M# 0%B %I"A5C2/0M#<++-78"l'"G52
1'(&(2'6D,@<:(/($E .0%C",8 D/"IH"5%$@1,+1'(&(2'6D,@<:( %B:;"I"A5C2/0M#<++/2'6/A6
,B@G:&/6C,8"D/"7/
PQ C,8"D/"<(1:;"1'(:(1')I7B/D,@<:( C,8"D/""%.8 :;"1'(./6"78/ %B9"4"H@$
@'0.='-7B/1K+H'C/6D='<4"H6/2'6/A6 %B 1K+C2/0M#&0'IA19"4"H@$@'0.=' <DH
C,8"D/""%8.$,6G0H0%1'(./6"78/ %B9"4"H@$@'0.=' -7B/9I21K+H'C2/0M#&0'IA1
C/6/($E<DH#D,@5,6",8"H'C/6D,@<:(9"C,8"D/""%8.:;"null
Q C,8"D/" &%B /6:;"1'(&(2'6D,@<:(-7B/+/1.='"@"&0'IA1C/6/($E<#./6"78/
%B9"4"H@$@'0.='&='4(,+1K+H'C2/0M#&0'IA1C/6/($E<DH#D,@ !5$9I2='&,B6
new IH"5%$@1,+1'(&(2'6//:.KC/6#'& C,8"D/""%8. ='942H'C/6D,@
<:(:#%B$"G::;"H'C/6D='<4"H6/2'6/A6 %B 1K+C2/0M#&0'IA1C/6/($E


8 ''f)  "+ #)0>


1'(:(1')I7B/C/6D,@<:(/($E.0%(M:<++#2'$1,+1'(:(1')I7B/D,@<:(C/6I"A5
C2/0M#<++-78"l'" <DH1'(:(1')D,@<:(/($E.D2/60%(7B/640'$ [] /$MH52'"4"2'4(7/
52'"4#,6I7B/D,@<:(5,6"%8


 [modifier] dataType []variableName;


4(7/ [modifier] dataType variableName[];

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



D,@/$H'6IH"='&,B6
  int x[];
private char []ch;
public double y[];
DH'61K:;"1'(:(1')942D,@<:( x, ch<#y:;"D,@<:(/($E!5$0%I"A5C2/0M#
:;" int,char<#doubleD'0#='5,+
1'(:(1')I7B/D,@<:($,6:;"1'(./6"78/ %B9"4"H@$@'0.=' -7B/1K+H'C/6D='<4"H6
/2'6/A6 %B 1K+C2/0M#&0'IA19"4"H@$@'0.=' RSB69"C,8"D/""%8.1K+H':;" null G@21H/"
"7B/6.'1$,6G0H0%1'(./6"78/ %B9"4"H@$@'0.='-7B/1K+C2/0M#&0'IA1C/6/($E
>'?'.'@'<D1DH'6.'1>'?'R%D(6 %B/"3n'D9429I2(7B/640'$ [] /$MH52'"4"2'I7B/
D,@<:(/($E %BD2/61'(:(1')G52 &H@"C2/<D1DH'6C/61'(9I2(7B/640'$ [] 52'"4"2'<#
52'"4#,6.1A5CS8"9"1(% %BD2/61'(:(1')D,@<:(4#'$FD,@D,@/$H'6IH"='&,B6
  int []x,y;
.:;"1'(:(1')942D,@<:(  x <# y :;"D,@<:(/($EI"A5C2/0M# int ,86MH<DH
='&,B6
  int x[],y;
.:;"1'(:(1')942D,@<:( x :;"D,@<:(/($E 0%B %I"A5C2/0M#:;" int &H@"D,@
<:(y :;"D,@<:(:1DA %BG0H9IHD,@<:(/($E!5$0%I"A5C2/0M#:;"int


8 ' "+ #)0>


1'(:(1')I7B/D,@<:(/($E.G0H0%1'(./6"78/ 9%B "4"H@$@'0.=' -7B/1K+C2/0M#
&0'IA1C/6/($E "78/ %B9"4"H@$@'0.='5,61#H'@.*M1./6CS8"07B/0%1'(9I2='&,B6 new RSB60%
(M:<++='&,B65,6"%8


 variableName = new dataType[size];




!5$ %B size7/.='"@"&0'IA1C/6/($E %BD2/61'(


='&,B6"%8 :;"1'((+3.='"@"&0'IA1C/6D,@<:(/($E<#:;"1'(./6"78/ %B9"4"H@$
@'0.='&='4(,+&0'IA1C/6/($E<DH#D,@ "/1.'1"%8$6, . ='942H'9"4"H@$@'0.=' %B1K+
D='<4"H6/2'6/A6C/6D,@<:(",8":#%B$"H'.'1 null G::;"D='<4"H6 %B 1K+C2/0M#5,6<&569"(M:
%BzQD,@/$H'6IH"='&,B6
  x = new int[5];


    
    


ch = new char[4];
y = new double[6];
:;"='&,B6&(2'6D,@<:(/($E x, ch<# y9420I% "A5C2/0M#:;" int, char<#
double<#0%.='"@"&0'IA1C/6/($E H'1,+t`u<#…D'0#='5,+


  4!$1'(1K+D='<4"H6/2'6/A6C/6D,@<:(/($E

9"1'(&(2'6D,@<:(/($EdataType %B/$M9H "='&,6B new.D2/6:;"I"A5C2/0M#I"A5


5%$@1,"1,+I"A5C2/0M#C/6D,@<:(/($E5,6",8"='&,6B 
  int []x;
x = new double[4];
.S6:;"='&,B69"1'(&(2'6D,@<:(/($E %BG0H*M1D2/6 "7B/6.'1D,@<:(/($E x *M1
:(1')9420I% "A5C2/0M#:;" int <DH='&,B6 new :;"1'(0'&(2'6/($EC/6C2/0M#I"A5
double
('&'0'(* %B.(@0='&,6B :(1')I7B/D,@<:(<#='&,6B 1'(&(2'6D,@<:(/($E G@29"
='&,B65%$@1,"G52!5$0%(M:<++='&,B65,6"%8


 dataType []variableName = new dataType[size];


4(7/ dataType variableName[] = new dataType[size];

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



D,@/$H'6IH"
 int []x = new int[5];

8" '+'D) )'+


D,@<:(/($E &%B (2'6CS8".0%&0'IA1 0%B %40'$#CD,86<DH m."*S6 ŠqP 1'(/2'6/A6*S6
&0'IA1C/6/($E<DH#D,@.0%(:M <++5,6"%8


 variableName[index]


!5$ %B index7/D,@#C(+340'$#C C%B /6&0'IA1C/6/($E RSB6.0%H'G52D,86<DHH'


m."*S6ŠqP
D,@/$H'6IH"
  x[3]
40'$*S6C2/0M#C/6&0'IA140'$#C %BcC/6D,@<:(/($Ex
D,@<:(/($E %B :;"I"A5C2/0M#<++-78"l'" .<D1DH'6.'1D,@<:(:1DA %B :;"I"A5
C2/0M#<++-78"l'" !5$D,@<:(/($E.1='4"5H'(A0B D2"9421,+&0'IA1 31D,@&0/ RSB60%H'
(AB0D2"IH"5%$@1,+D,@<:( %B :;"3#,1?C/6//:.K D'('6 %B Qt C %BD@, <:(:1DA.
*M11='4"5H'(AB0D2"|-'D,@<:( %B:;"3#,1?C/6//:.K 4(7/3#,1?C/6#'&
 H'",8"
('&'0'(* %B.:#%$B "<:#6H'C/6&0'IA1C/6/($EG52 !5$1'(9I2='&,B61='4"5H'
!5$D2/6/2'6/A6*S640'$#C&0'IA1C/6/($ED,@/$H'6IH"
  x[0] = 4;
x[2] = 5;
:;"1'(1='4"5H'9421,+&0'IA140'$#C %Bm<#C/6D,@<:(/($Ex9420%H':;"
u<#tD'0#='5,+
1'(/2'6/A6*S6I7B/D,@<:( b !5$G0H(+340'$#C&0'IA1.:;"1'((%$15MH'D='<4"H6
/2'6/A6C/6D,@<:(/($E !:(<1(0 %B zQP <&56D,@/$H'61'(:(1')<#&(2'6D,@<:(/($E
-(2/0 ,861='4"5H'DH'6F!5$W##,-LE %BG52:;"5,6<&569"(M: %BzQc





    
    


("'  4! D,@/$H'61'(9I2/($E


 public class SimpleArrays {
 public static void main(String args[]) {
int []x;
 x = new int[4];
x[0] = 0;
 x[1] = 2;
x[2] = 4;
 x[3] = 6;
 System.out.println("x = "+x);
System.out.println("x[0] = "+x[0]);
 System.out.println("x[1] = "+x[1]);
System.out.println("x[2] = "+x[2]);
 System.out.println("x[3] = "+x[3]);
}
 }



  4!%W##,-LE G%B 52.'11'((,"!:(<1(0 %BzQP




8& ''A&0
1 D&' )'+ 
('&'0'(* %B.:(1')D,@<:(/($E &(2'6D,@<:(/($E <#1='4"5H'9421,+
&0'IA1C/6/($E>'$9"='&,B65%$@1,"!5$0%(M:<++C/6='&,B65,6"%8


dataType []variableName = {value1,value2,..,valueN};


!5$ %B value1,value2,..,valueN :;"H' D%B 2/61'(1='4"59421,+&0'IA1
C/6/($E<DH#D,@ RSB6.D2/6:;"C2/0M#H'6 %B 0%B %I"A5C2/0M# &%B /5#2/61,+I"A5C2/0M#C/6D,@
<:(/($E
D,@/$H'6IH"

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



  int []x = {4,3,5,1,8};
:;"='&,B6&(2'6D,@<:(/($E x RSB60%.='"@"&0'IA1 t D,@ !5$ %B x[0],x[1],
x[2],x[3]<# x[4] 0%'H (AB0D2":;"u`c`t`P<#zD'0#='5,+

1'(1='4"5H'(AB0D2"C/6D,@<:(/($E ='G52|-'9"='&,6B :(1')D,@<:( H'",8"


,86"%8('G0H&'0'(* .%B 1='4"5H'(AB0D2">'$4#,6.'1='&,B6:(1')D,@<:(/($EG52 1#H'@7/
='&,B6
  int []x;
x[] = {4, 3, 5, 1, 8}; ‡‡

:;"='&,B6 G%B 0H*M1D2/6




8- 'D)
A for  )'+
!5$ @,B G:('.9I2='&,6B for9"1'(/2'6/A6*S6&0'IA1C/6/($E %BD2/6(%$19I29"='&,B6
%BR8='1,"/' AIH"='&,B6
  int x[] ={4,3,5,1,8};
.:;"1'(:(1')<#&(2'6D,@<:(/($E 0%B %&0'IA142'D,@ 4'1D2/61'(-A0-EH'
C2/0M#&0'IA1C/6/($E<DH#D,@!5$9I2='&,B65,6"%8
  System.out.println(x[0]);
  System.out.println(x[1]);
System.out.println(x[2]);
System.out.println(x[3]);
System.out.println(x[4]);
.4K"G52@H':;"1'((%$19I2='&,6B %BR8='1," RSB6('&'0'(*.< " %B52@$='&,B6 for
-7B/942!:(<1(01(I,+CS8"5,6"%8
  for(int i = 0; i < 5; i++) {
System.out.println(x[i]);
  
,86"%8>'?'.'@'1='4"5942D@, <:(/($E 31D,@0%3#,1? length -7B/(+3
.='"@"&0'IA1C/6/($E<DH#D,@RSB6D,@/$H'6C2'6D2". ='942 x.length0%H':;"t5,6",8"
&H@"C/6!:(<1(0C2'6D2"&'0'(*C%$"940HG52:;"
  for(int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}


    
    


!:(<1(0 %B zQ<&56D,@/$H'61'(9I2='&,B6 for1,+D,@<:(/($E-7B/1='4"5H'<#


-A0-EH'C/6&0'IA1C/6/($E<DH#D,@ RS6B W##,-LE %BG52.:;"IH"5%$@1,"1,+W##,-LE %BG52.'1
!:(<1(0 %BzQP

("'  4!$D,@/$H'61'(9I2='&,B6for

public class ForWithArrays {

public static void main(String args[]) {
int []x;
 x = new int[4];
for (int i=0; i<x.length; i++) {
 x[i] = i*2;
}
 System.out.println("x = "+x);
for (int i=0; i<x.length; i++) {
 System.out.println("x["+i+"] = "+x[i]);
 }
}

 } 

84 g0#0ArrayIndexOutOfBoundsException
1'(/2'6/A6*S640'$#C&0'IA1C/6/($E %BG0H*M1D2/6 . ='9421A5C2/WA5-#'59"D/"
(,"!:(<1(0!5$!:(<1(0.&H6C2/WA5-#'5 ArrayIndexOutOfBoundsException
//10'9"C(,"!:(<1(0
D,@/$H'6IH"='&,B6
  int[]x = {4,3,5,1,8};
:;"='&,B6:(1')<#&(2'6D,@<:(/($E %B0.% ='"@"&0'IA1D,86<DH40'$#C m *S6 u
1'(/2'6/A6*S6&0'IA1D,@/7B"IH"x[5]. ='9421A5C2/WA5-#'59"D/"(,"!:(<1(0
!:(<1(0 %B zQc <&56D,@/$H'6!:(<1(0 0%B %1'(/2'6/A6&0'IA1C/6D,@<:(/($E %BG0H
*M1D2/6RSB6W##,-LE %BG52:;"5,6<&569"(M: %BzQu





   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



("'  4!%D,@/$H'6!:(<1(0 %B.&H6C2/WA5-#'5//10'
 public class IncorrectArrays {
public static void main(String args[]) {
 int []x = {4,3,5,1,8};
 }
System.out.println(x[5]);

 }





  4!,W##,-LE G%B 52.'11'((,"!:(<1(0 %BzQc


8" + #)0


#
D,@<:(/($EC/6C2/0M#I"A5#'&7/ D,@<:( 9%B I21K+1#30H C/6C2/0M#&0'IA1 %B :;"
//:.KC/6#'&95#'&4"SB6
C,8"D/"1'(&(2'6D,@<:(/($EC/6C2/0M#I"A5#'&.0%C,8"D/"#2'$1,+C,8"D/"1'(
&(2'6D,@<:(/($EC/6C2/0M#I"A5-78"l'"!5$0%C,8"D/"DH'6F5,6"%8
PQ C,8"D/"<(1:;"1'(:(1')D,@<:(/($ERSB6../6"78/ %B9"4"H@$@'0.=' -7B/
1K+H'C/6D='<4"H6/2'6/A6C/6D='<4"H69"4"H@$@'0.='C/6&0'IA1C/6/($E<DH
#D,@
Q C,8"D/" &%B /6:;"1'(9I2='&,B6 new -7B/&(2'6<#./6"78/ 9%B "4"H@$@'0.='-7B/
1K+H'C/6D='<4"H6/2'6/A6C/6&0'IA1C/6/($ERSB6.I%8G:$,6D='<4"H6 %B 1K+C2/0M#
.(A6F
cQ C,8"D/"&35 2'$:;"1'(&(2'6//:.KC/6#'&9421,+&0'IA1<DH#D,@C/6/($E
!5$9I2='&,B6 new RSB6.:;"1'(./6"78/ %B9"4"H@$@'0.='-7B/1K+C2/0M#.(A6F
C/6//:.K
D,@/$H'6IH"*2'#'&Student0%"A$'05,6"%8
 public class Student {


    
    


private String name;


public Student() {
name = “NoName”;
}
 public Student(String n) {
name = n;
}
public String getName() {
return name;
}
}
('&'0'(* %B.:(1')D,@<:(/($E s 9420%I"A5C2/0M#:;"#'& Student D'0
C,8"D/"<(1!5$9I2='&,B65,6"%8
  Student []s;
C,8"D/" &%B /6:;"1'(1='4"5C"'5<#&(2'6D,@<:(/($E RSB64'1D2/61'(9420%
.='"@"&0'IA1cD,@. ='G52!5$9I2='&,6B 5,6"%8
 s = new Student[3];
C,8"D/" &%B '0.:;"1'(/2'6/A6&0'IA1C/6/($E<DH#D,@ 4(7/&(2'6//:.KC/6
#'&Student!5$9I2='&,B6new-7B/:;"1'((%$19I2X
X 407/"1,+1'(&(2'6//:
.K ,B@G:5,6"%8
  s[0] = new Student(“Thana”);
s[1] = new Student(“Somchai”);
s[2] = new Student(“Somsak”);
/"SB6('&'0'(* .%B 9I2='&,B6for9"1(% %BD2/61'(.&(2'6//:.KC/6&0'IA1<DH#
D,@!5$(%$19I2X
X <++V
G525,6"%8
  for(int i=0, i<s.length; i++) {
s[i] = new Student();
}
"/1.'1"%8('$,6&'0'(* .%B (@0='&,6B 9%B I29"1'(:(1')<#&(2'6D,@<:(/($EC/6
C2/0M#I"A5#'& <#='&,B6 %B9I29"1'(&(2'6//:.K9421,+&0'IA1C/6/($E<DH#D,@
5,6D,@/$H'6DH/G:"%8
  Student []s = {new Student(“Thana”);
new Student(“Somchai”);
new Student(“Somsak”)};

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6




8" ''*
1 "+ #)0
#
D,@<:(C/6C2/0M#I"A5#'&!5$ ,B@G:.:;"D,@<:(<++/2'6/A6/' AIH"='&,B6
 Student s1 = new Student(“Somsri”);
.:;"1'(&(2'6D,@<:(<++/2'6/A6 s1 RSB6H' %B s1 1K+9"4"H@$@'0.='.:;"
D='<4"H6 %B/2'6/A6G:$,6"78/ %B9"4"H@$@'0.=' %B 1K+C2/0M#C/6//:.KC/6#'&Student
 1(%C/6D,@<:(/($EC/6C2/0M#I"A5#'& C2/0M#&0'IA1C/6/($E<DH#D,@1K.1K+
D='<4"H6/2'6/A6G:$,6"78/ 9%B "4"H@$@'0.=' %B 1K+C2/0M#C/6//:.KC/6#'& Student <DH
#//:.K
 (M: %BzQt <&56D,@/$H'61'(1K+H'9"4"H@$@'0.='C/6D,@<:(s1<#D,@<:(/($Es
%B&(2'6CS8".'1='&,6B C2'6D2"



  4!-D,@/$H'61'(1K+H'C/6D,@<:(9"4"H@$@'0.='

!:(<1(0 %B zQu <&56D,@/$H'6!:(<1(0 0%B %1'(&(2'6D,@<:(/($EI"A5C2/0M##'&
Student<#0%1'((%$19I20L/5getName() C/6//:.K<DH#D,@RSB6W##,-LE %BG52:;"
5,6<&569"(M: %BzQ…





    
    


("'  4!,D,@/$H'6/($EC/6C2/0M#I"A5#'&
 public class Student {
private String name;
 public Student(String n) {
name = n;
 }
public String getName() {
 return name;
}
 }
 -------------------------------------------------------
public class TestClassArrays {
 public static void main(String args[]) {
Student []s = {new Student(“Thana”),
 new Student(“Somchai”),
new Student(“Somsak”)};
 for(int i=0; i<s.length; i++) {
System.out.print(s[i].getName()+" ");
 }
 }
}

  4!.W##,-LE G%B 52.'11'((,"!:(<1(0 %BzQu




8& +&#+ 


>'?'.'@'1='4"59420%D@, <:(/($E %B:;"4#'$0ADA/' AIH" 1'(1='4"5D,@<:( 0%B %
#,1?:;"0D(A1RE  b 4(7/D'('6.D2/69I2D,@<:(/($E %B :;"&/60ADA  1'(:(1')
D,@<:(/($E 0%B %C"'50'11@H'4"SB60ADA ='G52!5$1'(-AB0(7B/640'$ › œ 9"<DH#0ADA 5,6",8"
(M:<++1'(:(1')D,@<:(/($E&/60AD0A %5,6"%8

[modifier] dataType [][]variableName;


4(7/ [modifier] dataType variableName[][];


   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



D,@/$H'6IH"
 int [][]x;
:;"1'(:(1')D,@<:(/($E&/60ADAxRSB60%I"A5C2/0M#:;"int
1'(&(2'6D,@<:(/($E4#'$0AD.A D2/6(+3.='"@"&0'IA1C/6/($E9"<DH#0AD!A 5$
9I2='&,B6new5,6",8"(M:<++1'(&(2'6D,@<:(/($E&/60ADA:;"5,6"%8

variableName = new dataType[row][col];

!5$ %B
x row7/.='"@"&0'IA19"<DH#<*@

x col7/.='"@"&0'IA19"<DH#/#,0"E
D,@/$H'6IH"
  x = new int [3][4];
:;"1'(&(2'6D,@<:(/($E&/60ADAxRSB60%C"'5c<*@u/#,0"E
1'((%$19I2&0'IA1C/6/($E&/60AD.A D2/6(+3D='<4"H6C/6<*@<#/#,0"E !5$0%
(M:<++5,6"%8

variableName[row_number][col_number]

!5$ %B 
x row_number7/40'$#C<*@C/6&0'IA1C/6/($E&/60ADA

x col_number7/40'$#C/#,0"EC/6&0'IA1C/6/($E&/60ADA
D,@/$H'6IH"
  x[2][3]
40'$*S6&0'IA1C/6/($EbD='<4"H6<*@ %B/#,0"E %Bc:;"D2"


8& '+("'  0'' 'M


!:(<1(0 '652'"AD)'&D(E %B 1%B$@C2/61,+1'(.,51'(0D(A1RE .D2/69I2D,@<:(
/($EC"'5&/60ADA !:(<1(0 %B zQt :;"D,@/$H'6<&561'(+@1<#M0D(A1RE&/60D(A1RE


    
    


!5$1'(+@10D(A1RE&/60D(A1RE",8"0D(A1RE ,86&/6.D2/60%C"'5 H'1," W##,-LE G%B 52.:;"


1'((@0&0'IA1<DH#D,@C/60D(A1REC2'52@$1,"/' AIH"

ªa 11 a 12 a 13 a 14 a 15 º ªb 11 b 12 b 13 b 14 b 15 º
«a a 22 a 23 a 24 »
a 25 » «b b 22 b 23 b 24 b 25 »»
« 21 « 21
«a 31 a 32 a 33 a 34 a 35 » j «b 31 b 32 b 33 b 34 b 35 » “
« » « »
«a 41 a 42 a 43 a 44 a 45 » «b 41 b 42 b 43 b 44 b 45 »
«¬a 51 a 52 a 53 a 54 »
a 55 ¼ «¬b 51 b 52 b 53 b 54 b 55 »¼


ªa 11  b 11 a 12  b 12 a 13  b 13 a 14  b 14 a 15  b 15 º
«a  b 21 a 22  b 22 a 23  b 23 a 24  b 24 a 25  b 25 »»
« 21
«a 31  b 31 a 32  b 32 a 33  b 33 a 34  b 34 a 35  b 35 »
« »
«a 41  b 41 a 42  b 42 a 43  b 43 a 44  b 44 a 45  b 45 »
«¬a 51  b 51 a 52  b 52 a 53  b 53 a 54  b 54 a 55  b 55 »¼

&H@"1'(M0D(A1RE N 1,+0D(A1RE e ",8".='"@"/#,0"EC/60D(A1RE N .D2/60%
C"'5 H'1,+.='"@"<*@C/60D(A1RE e !5$*2'0D(A1RE  :;"W##,-LE %BG52.'11'(M
0D(A1RE5,6"%8

ªc 11 c 12 c 13 c 14 c 15 º
«c c 22 c 23 c 24 c 25 »»
« 21
«c 31 c 32 c 33 c 34 c 35 » “
« »
«c 41 c 42 c 43 c 44 c 45 »
«¬c 51 c 52 c 53 c 54 c 55 »¼


ªa 11 a 12 a 13 a 14 a 15 º ªb 11 b 12 b 13 b 14 b 15 º
«a a 22 a 23 a 24 a 25 »» «b b 22 b 23 b 24 b 25 »»
« 21 « 21
 «a 31 a 32 a 33 a 34 a 35 » b «b 31 b 32 b 33 b 34 b 35 » 
« » « »
«a 41 a 42 a 43 a 44 a 45 » «b 41 b 42 b 43 b 44 b 45 »
«¬a 51 a 52 a 53 a 54 a 55 »¼ «¬b 51 b 52 b 53 b 54 b 55 »¼

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



.G52&0'IA1C/60D(A1RE <DH#D,@0%H'5,6"%8
cij = ai1x b1j + ai2x b2j + ai3x b3j + ai4x b4j + ai5x b5j 
!:(<1(0 %B zQt.0%='&,B6 int[][]a = new int[5][5]; %B9I29"1'(&(2'6
D,@<:(/($E&/60ADA !5$0%H'C/6&0'IA1C/6/($E.'11'(&30H D,@#C.='"@"(4@H'6 m *S6 s
.'1='&,6B
  a[i][j] = (int)(Math.random()*10);
!:(<1(0"%8.9I2='&,6B  for %B0%!(6&(2'6<++R2/"/$MH4#'$ %B ,86"%8 -7B/IH@$9"1'(
.,51'(:(0@#W#C2/0M#C/6&0'IA1D,@<:(/($E
#'&Matrices0%0L/55,6"%8
x addMatrices(int[][] m1, int[][] m2)9I2-7B/='"@4'W##,-LE
BG% 52.'11'(+@10D(A1RE !5$(,+  
 C2'0':;"D,@<:(/($E&/60ADA 
D,@
x mulMatrices(int[][] m1, int[][] m2) 9I2-7B/='"@4'
W##,-LE G%B 52.'11'(M0D(A1RE !5$(,+  
 C2'0':;"D,@<:(/($E&/6
0ADAD,@
x printMatrix(int[][] m) 9I2-7B/<&56H'&0'IA1<DH#D,@C/60D(A1RE %B
(,+C2'0':;" 

 D,@/$H'6W##,-LE %BG52.'1!:(<1(0"%8:;"5,6<&569"(M: %BzQv


    
    


("'  4!-D,@/$H'61'(='"@0D(A1RE
 class Matrices {
int [][]m = new int[5][5];
 public int[][] addMatrices(int[][] m1, int[][] m2) {
for (int i=0; i<m1.length; i++) {
 for (int j=0; j<m1[i].length; j++) {
m[i][j] = m1[i][j] + m2[i][j];
 }
}
 return m;
 }
public int[][] mulMatrices(int[][] m1, int[][] m2) {
 int sum;
for (int i=0; i<m1.length; i++) {
 for (int j=0; j<m1[i].length; j++) {
sum = 0;
 for (int k = 0;k < m1[i].length; k++) {
sum += m1[i][k] * m2[k][j];
 }
 }
m[i][j] = sum;

 }
return m;
} 
public void printMatrix(int[][] m) {
 for (int i=0; i<m.length; i++) {
for (int j=0; j<m[i].length; j++) {
 System.out.print(m[i][j]+"\t");
}
 System.out.println();
 }
}
} 
public class CalMatrices {

public static void main(String args[]) {
int [][]a = new int[5][5];
 int [][]b = new int[5][5];
 int [][]c = new int[5][5];
for (int i=0; i<a.length; i++) {
 for (int j=0; j<a[i].length; j++) {
a[i][j] = (int) (Math.random()*10);
 b[i][j] = (int) (Math.random()*10);
}
 }
Matrices mt = new Matrices();
 System.out.println("Matrix A:");


   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



  mt.printMatrix(a);
System.out.println("Matrix B:");
mt.printMatrix(b);
c = mt.addMatrices(a,b);
System.out.println("Matrix A+B:");
mt.printMatrix(c);
c = mt.mulMatrices(a,b);
System.out.println("Matrix A*B:");
mt.printMatrix(c);
}
}

  4!3 D,@/$H'6W##,-LE %BG52.'11'((,"!:(<1(0 %BzQt

/"SB63#,1? length 07B/"='0'9I21+, D,@<:(/($E&/60AD.A 942H'5,6"%8


x.length .G52H' H'1,+.='"@"<*@C/6D,@<:(/($E &H@" x[i].length .G52H'
 H'1,+.='"@"/#,0"E9"<*@ %BiC/6D,@<:(/($E


    
    


8& +  A 


# 1'
>'?'.'@'/"3n'D9420%1'(&(2'6/($E&/60ADA %B0%C"'5C/6/#,0"E9"<DH#<*@G0H
 H'1,"G52!5$.D2/6(+3.='"@"<*@!5$9I2='&,B6new1H/"<#2@(+3.='"@"/#,0"E9"<DH#
<*@D,@/$H'6IH"='&,B6
int [][]x;
x = new int[3][];
x[0] = new int[4]
x[1] = new int[2];
x[2] = new int[3]
:;"1'(&(2'6D,@<:(/($E x  RSB60%&0'IA1.='"@" c <*@  !5$ <%B *@ %B4"SB60% u
/#,0"E<*@ &%B /60%/#,0"E<#<*@ %B&'00%c/#,0"E
!:(<1(0 %B zQ… <&561'(&(2'6D,@<:(/($E&/60ADA %B<DH#<*@0%.'= "@"/#,0"E
DH'61," !:(<1(0"%.8 G52W##,-LE5,6<&569"(M: %B zQz <#(M: %B zQs<&56C2/0M# %B/$M9H "&0'IA1
<DH#D,@C/6/($E

("'  4!.D,@/$H'61'(&(2'6/($E&/60ADA %B<DH#<*@0%.='"@"/#,0"EDH'61,"



public class TwoDimensionArrays {
 public static void main(String args[]) {
 int x[][] = new int[3][];
x[0] = new int[4];
 x[1] = new int[2];
 x[2] = new int[3];
for(int i=0; i<x.length; i++) {
 for(int j=0; j<x[i].length; j++) {
x[i][j] = (i+j)*2;
 }
 }
 for(int i=0; i<x.length; i++) {
 for(int j=0; j<x[i].length; j++) {
 System.out.print(x[i][j]+" ");
 }

System.out.println();


}
 }
 }





   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6






  4!4 W##,-LE %BG52.'11'((,"!:(<1(0 %BzQ…


  4!5 <&56D,@/$H'6C2/0M# /%B $MH9"/($ERSB6:;"W##,-LE %BG52.'11'((,"!:(<1(0 %BzQ…




8-  G0 ' + '+


>'?'.'@'0%0L/54#'$0L/5 %B(,+  
 :;"/($EC/6C2/0M#I"A5DH'6F
D,@/$H'6IH"<-1K. java.lang0%#'& I%B 7B/ ArraysRSB60%0L/5 %B 1%$@C2/61,+/($E %B
&=',n5,6"%8
x sort():;"0L/5 9%B I29"1'((%$6H'C2/0M#&0'IA1C/6/($E.'1"2/$G:0'1
x binarySearch() :;"0L/5 %B9I29"1'(2"4'H'C2/0M# %BD2/61'(.'1I35
C2/0M# ,86405C/6&0'IA1C/6/($ERSB6*M1(%$6.'1"2/$G:0'14(7/0'1G:"2/$
G@2<#2@
x fill() :;"0L/5 %B9I29"1'(1='4"5H'C2/0M#5%$@1,"9421,+&0'IA1 ,86405
C/6/($E
"7B/6.'10L/5 ,86&'00L/5 1%B #H'@0'C2'6D2":;"0L/5<++ static 5,6",8"1'(
(%$19I26'"0L/54#H'"%8.S6&'0'(*(%$1!5$9I2I7B/#'& ArraysG52#$G0H.=':;"D2/6&(2'6
//:.KC/6#'&ArraysCS"8 0'1H/"1'((%$19I26'"
!:(<1(0 %B zQv <&56D,@/$H'61'((%$19I20L/5C/6#'&  Arrays %B1#H'@G@2
C2'6D2"!:(<1(0"%8 (%$19I20L/5sort()!5$&H6 
 %B:;"D,@<:(/($EdG:


    
    


-7B/(%$6H'C2/0M#C/6&0'IA1C/6D,@<:(/($E  d  .'1"2/$G:0'1  0L/5


binarySearch()  .9I29"1'(2"4'&0'IA1C/6D,@<:(/($E  d  %B0%H' H'1,+ PQ…t
&H@"0L/5fill()9I29"1'(1='4"5H'C/6&0'IA1C/6D,@<:(/($E d 31D,@9420%H':;"
PQm!:(<1(0"%.8 942W##,-LE5,6<&569"(M: %BzQPm
>'?'.'@'G0H$/09420%1'(:#%B$"<:#6C"'5C/6/($E 5,6","8 1'(9I2='&,B6 new
-7B/:(1')C"'5C/6D,@<:(/($E940H. ='942H'C/6C2/0M#5A04'$G:D,@/$H'6IH"='&,B6
int []x = {4,7,9};
x = new int[4];
 .:;"='&,6B 9"1'(:(1')D,@<:(/($E x <#&(2'6/($E-(2/01,+1='4"5H'
(AB0D2"9421,+&0'IA1 6,8  D,@C/6/($E!5$0%H'C2/0M#:;"I#<#* D'0#='5,+<DH='&,B6
*,50'.:;"1'(&(2'6/($E x CS8"0'940H!5$G0H&'0'(*"='H'C/6C2/0M#5A01#,+0'G52 .S6 '=
942H'C2/0M#&0'IA1C/6/($E ,86 D,@0%H' H'1,+9 ,86405

("'  4!3D,@/$H'61'((%$19I20L/5C/6#'&Arrays

import java.util.Arrays;

public class MethodsArrays {


public static void main(String args[]) {
double d[] = {5.3, 3.56, 0.5, 1.65, 7.8};
Arrays.sort(d);
for(int i=0; i<d.length; i++) {
System.out.print(d[i]+" ");
}
System.out.println();
int pos = Arrays.binarySearch(d,1.65);
System.out.println("arrays["+pos+"] = 1.65");
Arrays.fill(d,1.0);
for(int i=0; i<d.length; i++) {
System.out.print(d[i]+" ");
}
System.out.println();
}
}

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6




  4! 6 W##,-LE %BG52.'11'((,"!:(<1(0 %BzQv

 >'?'.'@'0%='&,6B  System.arraycopy() 9I2&='4(,+,5#/1H'C2/0M#C/6


&0'IA1C/6/($E!5$0%!(6&(2'6(M:<++='&,6B 5,6"%8
 System.arraycopy(Object src,int src_pos,
Object dst,int dst_pos,int length);
 !:(<1(0 %B zQz <&56D,@/$H'61'(9I2='&,6B   System.arrayCopy() !5$&H6
,0^2;871 RSB6:;"D,@<:(/($E scr RSB6:;"/($ED2"<++ (A0B .'1H'C2/0M#C/6&0'IA1
#='5,+ %B  C/6/($E src G:$,6&0'IA1#='5,+ %B 0 C/6/($E dst !5$942 ='1'(,5#/1
C2/0M# ,86&A8"H'C2/0M#!5$!:(<1(0"%.8 G52W##,-LE5,6<&569"(M: %BzQPP

("'  4!4D,@/$H'61'(9I2='&,B6System.arraycopy();
 public class CopyArrays {
 public static void main(String args[]) {
String []scr = {"Copy","an","array","from",
 " source"," to"," destination."};
 String []dst = new String[4];
System.arraycopy(scr,3,dst,0,4);
 for(int i=0; i<dst.length; i++) {
 System.out.print(dst[i]);
 }
 System.out.println();
 }
 }








  4! W##,-LE %BG52.'11'((,"!:(<1(0 %BzQz


    
    


84
##*') 
 D,@<:(/($E:;"I"A5C2/0M#<++/2'6/A6 RSB607B/0%1'(&(2'6/($E!5$9I2='&,B6 new
<#2@.G0H&'0'(*:#%$B "<:#6C"'5C/6.='"@"&0'IA1 %B 1K+9"/($EG52 9"1(% %BD2/61'(.
1K+1#3H0C/6C2/0M# &%B '0'(*:#%B$"<:#6C"'5C/6.='"@"&0'IA1G52 >'?'.'@'.9I2#'&
:(> /##K1I,B"  X RSB6:;"#'& %B9I29"1'(1K+1#30H C/6C2/0M#I"A5#'& I%B 7B/
Object  RSB6#'& 31F#'&9">'?'.'@'.&7+ /50'.'1#'& %BI7B/ Object "%8 5,6",8"
#'&:(> /##K1I,B"&'0'(*1K+//:.KC/6#'&95F9">'?'.'@'G52D'04#,11'(
 XeVC/61'(0%G524#'$(M:<++ .%B 1#H'@*S69"+ %Bu
#'&:(> /##K1I,B"*M11='4"5G@29" NO %BI7B/ _=33851/=7 PHQ!5$.
:(1/+G:52@$/A"D/(EJ&<##'&DH'6F5,6<&569"(M: %BzQP_=33851/=7PHQG521='4"5
/A"D/(EJ&DH'6F %B0%67B/"GC9"1'(9&HC2/0M#C/6&0'IA1C/6/##K1I,B" %B<D1DH'61,"5,6"%8
 x Collection:;"/A"D/(EJ& %B1='4"50L/59"1'(.,51'(C2/0M#C/6&0'IA1
&='4(,+#'&:(> /##K1I,"B %B.D2/6implements/A"D/(EJ&"%8
 x Set:;"/A"D/(EJ& %B9I2 implements#'&:(> /##K1I,B" 0%B &% 0'IA1
%B0%C2/0M#G0HR8='1,"<#G0H0#% ='5,+1'(9&HC2/0M#
 x List  :;"/A"D/(EJ& %B9I2 implements #'&:(> /##K1I,B" %B0%
&0'IA1RSB6/'.0%C2/0M#R8='1,"G52<DH.0%#='5,+C/6&0'IA1C/61'(9&HC2/0M#
 x Map:;"/A"D/(EJ& 9%B I2  implements#'&:(> /##K1I,B"RSB6&0'IA1
.0%/6E:(1/+&/6&H@"7/C2/0M#<#%$E w C2/0M#C/6&0'IA1:(> "%8/'.
R8='1,"G52<DH$% EC/6&0'IA1<DH#D,@.D2/6G0HR8='1,"
 #'&9" _=33851/=7 PHQ %B implements /A"D/(EJ&4#H'"%87/  HashSet,
ArrayList, LinkedList, Vector<#HashMap

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6





  4! $/A"D/(EJ&<##'&DH'6F9"_=33851/=7PHQ

84 2Collection
 Collection :;"I7B//A"D/(EJ& %B1='4"5G@29" _=33851/=7 PHQ !5$0%0L/5 %B
&=',n5,6"%8
 x boolean add(Object element)
  :;"0L/5 9%B I29"1'(9&H&0'IA1#69"/##K1I,B" !5$&0'IA1 .%B 9&HD2/6:;"
//:.KC/6#'&95#'&4"SB6  0L/5"%8.&H6H'1#,+0':;"I"A5C2/0M#<++
boolean!5$.942H':;"true*2'&'0'(*9&HC2/0M#G52

 x boolean remove (Object element)


  :;"0L/5 9%B I29"1'(#+&0'IA1//1.'1/##K1I,B" !5$D2/6&H6WH'"  

%B:;"//:.K D%B 2/61'(#+//1  0L/5"%8.&H6H'1#,+0':;"I"A5C2/0M#<++
boolean!5$.942H':;"true9"1(% %B#+C2/0M#G52

 x int size()
  :;"0L/5 9%B I29"1'(4'.='"@"&0'IA1 0%B %/$M9H "/##K1I,B"
 x boolean isEmpty()
  :;"0L/5 9%B I29"1'(D(@.@H'/##K1I,B"0%&0'IA1/$MH4(7/G0H !5$.&H6H'1#,+0'
:;"true*2'G0H0%&0'IA1/$M9H "/##K1I,B"
 x boolean contains(Object element)


    
    


  :;"0L/5 9%B I29"1'(D(@.&/+@H' /##K1I,B"0%&0'IA1 %B0%'H :;"//:.KC/6


 
 %B&6H WH'"0'4(7/G0H
 x Iterator iterator()
  :;"0L/5 9%B I29"1'(<.1<.6C2/0M#C/6&0'IA19"/##K1I,B"

84 2Set
Set  :;"/A"D/(EJ& B&% +7 /50'.'1/A"D/(EJ& Collection !5$0%#'& %B
&=',n %B  implements/A"D/(EJ&"%87/#'& HashSet#'& HashSet9I29"1'(
&(2'6//:.K:(> /##K1I,"B RSB6.0%&0'IA1C/6C2/0M#R8='1,"G0HG52 !:(<1(0 %B zQs <&56
D,@/$H'61'(9I2#'& HashSet !:(<1(0"%8.D2/60%='&,6B  import java.util.*;
-7B/ %B.(%$19I2#'&DH'6F %B1'= 4"59" _=33851/=7 PHQ !:(<1(0"%8G52&(2'6//:.K s RSB6
:;"//:.KC/6#'& HashSet<#(%$19I20L/5 add()9"1'(9&HC2/0M##69"//:.K
s!5$:1DA<#2@ 
C/60L/5add().D2/6:;"//:.KC/6#'& I %B 7B/Object
<DH!:(<1(0"%8.&H6  
 %B:;"//:.KC/6#'& String RSB6&'0'(* '= G52"7B/6.'1
#'& String:;"#'& &%B 7+ /50'.'1#'& I%B 7B/ Object!:(<1(0"%8D2/61'(.<&56
9424K"@H'('G0H&'0'(* %B.9&HC2/0M# %BR8='1,"C2/@'0€#69"//:.KI"A5HashSet
G52RSB6!:(<1(0"%.8 G52W##,-LE5,6<&569"(M: %BzQPc


("'  4!5 D,@/$H'61'(9I2#'&HashSet


import java.util.*;
public class SampleSet {
public static void main(String args[]) {
Set s = new HashSet();
s.add("C#");
s.add("Java");
s.add("Pascal");
System.out.println("The size of this set is "+
s.size());
System.out.println("The contents are "+s);
System.out.println("Removing C#");
s.remove("C#");
System.out.println("Now this set contains C#: "+
s.contains("C#"));
s.add("Java");
System.out.println("Now the size is "+s.size());
System.out.println("The contents are "+s);
}
}

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6




  4! % W##,-LE %BG52.'11'((,"!:(<1(0 %BzQs

84" 2List
 List  :;"/A"D/(EJ& & %B 7+ /50'.'1/A"D/(EJ& Collection  <DH.
<D1DH'6.'1/A"D/(EJ& Set  D(6 .%B 0%#'= 5,+C/6&0'IA1/$MH Vb  <#&'0'(* %B0%
&0'IA1RSB60%C2/0M#RSB6R8='1,"RSB6/A"D/(EJ&List.-AB00L/5 %B1%B$@C2/61,+C2/0M##='5,+C/6
&0'IA15,6"%8
 x void add(int index,Object element)
:;"0L/59"1'(9&H&0'IA1#69"/##K1I,B" %B :;"//:.K %B0%'H :;"  

%B&H6WH'"!5$0%#='5,+ %B :;"#C.='"@"DK09" 
 %BI7B/index
 x Object remove(int index)
  :;"0L/59"1'(#+&0'IA1#='5,+ %BRSB60%#C.='"@"DK09"  
  %BI7B/
index//1.'1/##K1I,B"

 x Object get(int index)


  0L/5"%89I29"1'((%$1C2/0M#C/6&0'IA1#='5,+ %B0%#C.='"@"DK09"  
 %B
I7B/index
 x int indexOf(Object element)
  0L/5"%89I29"1'(D(@.&/+@H'//:.K 0%B %H'9"  
 %B&H6WH'"0':;"
&0'IA1#='5,+ %B H'G(C/6/##K1I,B"
 x ListIterator listIterator()
  0L/5"%89I29"1'(<.1<.6C2/0M#C/6&0'IA19"/##K1I,B"<++List


    
    


 #'&&=',n %B implements/A"D/(EJ& List RSB6(+3G@29"_=33851/=7PHQ7/


#'& LinkedList <#  ArrayList  !:(<1(0 %B zQPm <&56D,@/$H'61'(9I2#'&
LinkedList!:(<1(0"%. 8 0%='&,6B l.get(0):;"1'((%$1C2/0M#C/6&0'IA1#='5,+ %B m
9"//:.Kl %B:;"//:.KC/6#'&LinkedList&H@"='&,B6l.indexOf(“Java”)
:;"='&,B6(%$15M#='5,+ C%B /6&0'IA19"//:.K l RSB60%C2/0M#:;"//:.K String %B0%
C2/@'09" “Java”  !:(<1(0"%8<&56D,@/$H'6C/6C2/<D1DH'6(4@H'6#'&:(>  Set
<#List ,86"%8.4K"G52.'1='&,B6l.add(“Java”)&'0'(* %B.9&HC2/0M# %BR8='1,"G52!5$
!:(<1(0.G52W##,-LE5,6<&569"(M: %BzQPu

("'  4! 6D,@/$H'61'(9I2#'&LinkedList
import java.util.*;

public class SampleList {


public static void main(String args[]) {
List l = new LinkedList();
l.add("C#");
l.add("Java");
l.add("Pascal");
System.out.println("The size is "+l.size());
System.out.println("The contents are "+l);
System.out.println("The first one is "+l.get(0));
l.add("Java");
System.out.println("The contents are "+l);
System.out.println("The index of Java is "+
l.indexOf("Java"));
}
}

  4! ,W##,-LE %BG52.'11'((,"!:(<1(0 %BzQPm




   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6




84& 2Map
 Map:;"/A"D/(EJ& & %B 7+ /50'.'1/A"D/(EJ&CollectionRSB6&0'IA1<DH#
D,@C/6/##K1I,B" %B :;"Map.0%C2/0M#/$MH&/6&H@"7/&H@" %B:;"%$E<#&H@" %B:;"C2/0M#RS6B
:;"//:.KC/6#'&95F%$E. ='4"2' I%B H@$9"1'(&7+2"&H@" %B :;"C2/0M#&0'IA1C/6 Map
!5$.D2/60%%$E %BG0HR8='1,"<DH/'.0%&H@" %B:;"C2/0M#R8='1,"G52 /A"D/(EJ& Map .0%0L/5
DH'6F %B -AB0CS8"0'-7B/IH@$&",+&"3"1'( ='6'"C/6%$E5,6"%8
 x Object put(Object key, Object value)
  :;"0L/5 9%B I29"1'(9&H&0'IA1#69"/##K1I,B"!5$D2/69&H//:.K ,86&H@" %B :;"
%$E<#&H@" %B :;"C2/0M#D'0 
 %BI7B/key<#valueD'0#='5,+
 x Object remove(Object key)
  :;"0L/5 9%B I29"1'(#+&0'IA1//1.'1/##K1I,B" !5$&0'IA1 %B*M1#+.0%H'
C/6%$E :;"//:.K %B0%H'D'0 
 %BI7B/key
 x Object get(Object key)
  :;"0L/5 9%B I29"1'((%$15MC2/0M#C/6&0'IA19"/##K1I,B" %B0% H'C/6%$E :;"H'
C/6//:.K %BI7B/key %BWH'"0'$,6 

 x Set entrySet()
  :;"0L/5 9%B I2(%$15MC2/0M#C/6&0'IA1 6,8 4059"/##K1I,B"
 x Set keySet()
  :;"0L/5 9%B I2(%$15M$% EC/6&0'IA1 ,864059"/##K1I,B"
 x int size()
  :;"0L/5 9%B I29"1'(4'.='"@"&0'IA1C/6/##K1I,B"
 #'& &%B =',n %Bimplements/A"D/(EJ&Map7/#'&HashMap!:(<1(0 %B
zQPP<&56D,@/$H'61'(&(2'6//:.KC/6#'&"%8<#2@(%$19I20L/5 put()9"1'(9&H&0'IA1
#69"/##K1I,B" ,86"%8 0L/5"%8.D2/6&H6WH'" 
&/6D,@9" %B"%8 6,8 &/6D,@.&H6WH'"//:
.KI"A5 String 0L/5 remove() 9" %B".%8 9I29"1'(#+&0'IA1 0%B %H'C/6%$E :;"
String %B0%H':;"€c!:(<1(0"%8.942W##,-LE5,6<&569"(M: %BzQPt


    
    


("'  4! D,@/$H'61'(9I2#'&HashMap


import java.util.*;

public class SampleMap {


public static void main(String args[]) {
Map m = new HashMap();
m.put("1","C#");
m.put("2","Java");
m.put("3","Pascal");
System.out.println("Removing Pascal");
m.remove("3");
System.out.println("The size is "+m.size());
System.out.println("The first one is "+
m.get("1"));
m.put("3","Java");
System.out.println("The key of this map are "+
m.keySet());
System.out.println("The contents are "+
m.entrySet());
}
}

  4! -W##,-LE %BG52.'11'((,"!:(<1(0 %BzQPP

84- 2Iterator
 Iterator :;"/A"D/(EJ& %B9I29"1'((%$15M&0'IA1C/6/##K1I,B" ,86"%8
/A"D/(EJ& Collection .0%0L/5 iterator() RSB6.&H6//:.KC/6#'&:(> 
Iterator 1#,+7"0' /A"D/(EJ& Iterator .0%0L/5DH'6F 9%B I29"1'((%$15MC2/0M#
5,6"%8
 x boolean hasNext()

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



  :;"0L/5 .%B D(@.&/+@H'$,60%C2/0M#/$M9H "Iterator/%14(7/G0H
 x Object next()
  :;"0L/5 .%B (%$15MH'C/6//:.KC/6&0'IA1D,@*,5G:C/6 Iterator!5$
.&H6H'1#,+0':;"//:.KC/6#'& %BI7B/Object
 x void remove()
  :;"0L/5 9%B I29"1'(#+&0'IA1D='<4"H6:]..3+,"//1.'1Iterator
 !5$ @,B G:D='<4"H6(AB0D2"C/6 Iterator .I%8G: D%B '= <4"H61H/"&0'IA1C/6
Iterator D,@<(1<#.9I20L/5 next() 9"1'((%$15M&0'IA1D,@DH/FG: !:(<1(0 %B
zQP <&56D,@/$H'61'(9I2/A"D/(EJ&  Iterator  9"1'((%$15MC2/0M#C/6&0'IA1C/6
//:.KI"A5ArrayList!:(<1(0"%8.942W##,-LE5,6<&569"(M: %BzQP…

("'  4! $D,@/$H'61'(9I2/A"D/(EJ&Iterator


import java.util.*;

public class SampleIterator {


public static void main(String args[]) {
Set scrSet = new HashSet();
scrSet.add("C#");
scrSet.add("Java");
scrSet.add("Pascal");
Iterator it = scrSet.iterator();
Set dstSet = new HashSet();
for(int i=0; i<scrSet.size(); i++) {
if(it.hasNext()) {
dstSet.add(it.next());
}
}
System.out.println(dstSet);
}
}

  4! .W##,-LE %BG52.'11'((,"!:(<1(0 %BzQP


    
    


 _=33851/=7 PHQ $,60%/A"D/(EJ&:(>  Iterator %B&=',n/%1&/6D,@7/


ListIterator <#  Enumeration  /A"D/(EJ&  ListIterator .9I29"1'(
(%$15MC2/0M#C/6/##K1I,B":(>  List !5$0%0L/5 &%B =',n %B-AB00'.'10L/5C/6
/A"D/(EJ&Iterator5,6"%8
 x boolean hasPrevious()
  :;"0L/5 .%B D(@.&/+@H'0%C/2 0M#9"D='<4"H61H/"4"2'"%89" ListIterator
4(7/G0H
 x Object previous()
  :;"0L/5 .%B (%$15MH'C/6//:.KC/6&0'IA1 9"D='<4"H61H/"4"2'"%8C/6
ListIterator!5$.&H6H'1#,+0':;"//:.KC/6#'& %BI7B/Object

 x void add(Object element)


  :;"0L/59"1'(9&H&0'IA1D,@940H#69"ListIterator
 x void set(Object element)
  :;"0L/5 9%B I29"1'(< "H'&0'IA1C/6 ListIterator 9"D='<4"H6:]..3+,"
52@$H' &%B H6WH'"0' '6 

 /A"D/(EJ& List.0%0L/5 listIterator()RSB6:;"0L/5 %B&H6H'1#,+
:;"//:.K:(>  ListIterator()  -7B/9I29"1'(<.1<.6C2/0M#C/6&0'IA1C/6
/##K1I,B":(> List
 /A"D/(EJ&  Enumeration  .0%#,1?#2'$1,+/A"D/(EJ& Iterator
!5$.9I29"1'(<.1<.6C/6C2/0M#C/6/##K1I,B"DH'6F /A"D/(EJ&"%8.0%0L/5 &%B =',n&/6
0L/57/
 x boolean hasMoreElement()
  :;"0L/5 9%B I29"1'(D(@.&/+@H'$,60%&0'IA19"Enumeration/%14(7/G0H
 x Object nextElement()
  :;"0L/5 9%B I29"1'((%$15MH'C/6&0'IA19" Enumeration %B/$MH9"D='<4"H6
*,5G:
 !5$ @,B G:1'((%$15MH'C/6&0'IA1C/6//:.K:(>  Enumeration .0%
(M:<++C/6='&,B65,6"%8
  while(e.hasMoreElements()){
System.out.print(e.nextElement()+“ ”);

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



}


844
#Vector
 Vector  :;"#'&:(> /##K1I,B" %B1='4"5G@29" _=33851/=7 PHQ #'&
Vector 9I21K+1#3H0C/6//:.KC/6#'&95F !5$G0H.='1,5.='"@" #'& Vector :;"
#'& %B implements /A"D/(EJ& List ('&'0'(*&(2'6//:.KC/6#'& Vector
!5$(%$19I2X
X 9"(M:<++DH'6F5,6"%8
x Vector()
x Vector(int initialCapacity)
x Vector(int initialCapacity, int capacityIncrement)
!5$ %B 
x initialCapacity7/C"'5(AB0D2"C/6Vector

x capacityIncrement7/C"'5 %B.-AB0CS8"07B/Vector0%C"'5DK0<#2@
 !:(<1(0 %BzQPc :;"D,@/$H'61'(9I2//:.KC/6#'&Vector<#9I2/A"D/(EJ&
Enumeration9"1'((%$15MC2/0M#!:(<1(0"%8.942W##,-LE5,6<&569"(M: %BzQPv

("'  4! %D,@/$H'61'(9I2//:.KC/6#'&Vector


 import java.util.*;

public class SampleEnumeration {
 public static void main(String args[]) {
Vector v = new Vector();
 v.add("C#");
 v.add("Java");
v.add("Pascal");
 Enumeration e = v.elements();
 while (e.hasMoreElements()) {
System.out.print(e.nextElement()+" ");

}
 }
 }




    
    


  4! 3 W##,-LE %BG52.'11'((,"!:(<1(0 %BzQPc

/&
x !5$ @,B G:!(6&(2'6C2/0M#<++/($E.*M1"='0'9I2 07B/D2/61'(1K+C2/0M#I"A5
5%$@1,"4#'$H'<DH9I2D,@<:(/($ED,@5%$@1,"
x /($E<+H6//1G52:;":(> 7//($EC/6C2/0M#I"A5-78"l'"<#/($EC/6
C2/0M#I"A5#'&C,8"D/"9"1'(&(2'6/($EC/6C2/0M#I"A5-78"l'"7/ 1'(:(1')
D,@<:(/($E<#1'(&(2'6/($E
x C,8"D/"9"1'(&(2'6/($EC/6C2/0M#I"A5#'&7/ 1'(:(1')D,@<:(/($E 1'(
&(2'6/($E<#1'(&(2'6//:.K9421,+&0'IA1C/6/($E<DH#D,@
x C"'5C/6/($E&'0'(*4'G52.'13#,1?length
x ='&,B6for"A$0"='0'9I29"1'(/2'6*S6&0'IA1C/6/($E
x ('&'0'(*&(2'6/($E4#'$0ADGA 52 !5$.='"@"(7B/640'$‹Œ+H6+/1*S6.='"@"
0ADCA /6/($E
x &='4(,+/($E&/60ADA.='"@"/#,0"E9"<DH#<*@G0H.=':;"D2/6 H'1,"
x 9"#'& I%B 7B/ Arrays
0%0L/5&=',n %B 1%$B @C2/61,+/($E7/ sort(),
binarySearch()<#fill() 

x 0L/5 arraycopy() .'1#'& System 9I29"1'(,5#/1H'C/6&0'IA1


C/6/($E
x Collection, Set, List <# Map :;"/A"D/(EJ&&=',n %B/$MH9"
_=33851/=7PHQ!5$ %B/A"D/(EJ& Set<# List&7+ /50'.'1/A"D/(EJ&
Collection
x /A"D/(EJ& Set .G0H&'0'(*0%H'C2/0M#C/6&0'IA1 %BR8='1,"G52 <#G0H0#% ='5,+
C/6&0'IA1

   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6



x &H@"/A"D/(EJ& List .&'0'(*0%H'C2/0M#C/6&0'IA1 %BR8='1,"G52 <#0%#='5,+
C/6&0'IA1
x &='4(,+/A"D/(EJ&Map.0%1'(1K+H'%$EMH1,+H'C2/0M#C/6&0'IA1!5$ %BH'%$E
C/6&0'IA1.D2/6G0HR8='1,"<DHH'C2/0M#C/6&0'IA1&'0'(*R8='1,"G52
x ('&'0'(*"='#'& %B implements /A"D/(EJ&4#H'"%8G:9I29"1'(1K+C2/0M#
%B:;"//:.KG524#'$D,@#2'$1,+/($E<DH&'0'(*:#%B$"<:#6C"'5G52
x /A"D/(EJ& Iterator,ListIterator <# Enumeration 9I29"1'(
/2'6*S6C2/0M#&0'IA1C/6#'& /%B $MH9" XNO
x #'& Vector :;"#'& %B9I29"1'(1K+1#30H C/6//:.KC/6#'&95F!5$G0H
.='1,5.='"@"



    
    















   !"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6

You might also like