Chap 8

  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Chap 8 as PDF for free.

More details

  • Words: 3,981
  • Pages: 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,"
  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.='
8 ''f)  "+ #)0> 1'(:(1')I7B/C/6D,@<:(/($E.0%(M:<++#2'$1,+1'(:(1')I7B/D,@<:(C/6I"A5 C2/0M#<++-78"l'"
 



[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 >'?'.'@''?'R%D(6 %B/"3n'D9429I2(7B/640'$ [] /$MH52'"4"2'I7B/ D,@<:(/($E %BD2/61'(:(1')G52 &H@"C2/
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
.S6:;"='&,B69"1'(&(2'6D,@<:(/($E %BG0H*M1D2/6 "7B/6.'1D,@<:(/($E x *M1 :(1')9420I% "A5C2/0M#:;" int


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


variableName[index]



!5$ %B index7/D,@#C(+340'$#C C%B /6&0'IA1C/6/($E RSB6.0%H'G52D,86
:;"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
 

 !"!#$%&'(&" )&*'+," !"!#$%-(./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


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 -7B/942!:(<1(01(I,+CS8"5,6"%8   for(int i = 0; i < 5; i++) {

for

System.out.println(x[i]);





 ,86"%8>'?'.'@'1='4"5942D@, <:(/($E 31D,@0%3#,1? length -7B/(+3 .='"@"&0'IA1C/6/($E
    

    





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

 !"!#$%&'(&" )&*'+," !"!#$%-(./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
    

    





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
/"SB6('&'0'(* .%B 9I2='&,B6for9"1(% %BD2/61'(.&(2'6//:.KC/6&0'IA1
"/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
 

 !"!#$%&'(&" )&*'+," !"!#$%-(./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
   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
    

    





("'  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"
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"
!5$ %B x

row7/.='"@"&0'IA19"
x col7/.='"@"&0'IA19"
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
a 12 a 22 a 32 a 42 a 52

a 13 a 23 a 33 a 43 a 53

a 14 a 24 a 34 a 44 a 54

ªa 11 «a « 21 «a 31 « «a 41 «¬a 51

 b 11  b 21  b 31  b 41  b 51

a 12 a 22 a 32 a 42 a 52

 b 12  b 22  b 32  b 42  b 52

a 15 º ªb 11 «b » a 25 » « 21 a 35 » j «b 31 « » a 45 » «b 41 «¬b 51 » a 55 ¼

b 12 b 22 b 32 b 42 b 52

b 13 b 23 b 33 b 43 b 53

b 14 b 24 b 34 b 44 b 54

b 15 º b 25 »» b 35 » “ » b 45 » b 55 »¼



a 13 a 23 a 33 a 43 a 53

 b 13  b 23  b 33  b 43  b 53

a 14 a 24 a 34 a 44 a 54

 b 14  b 24  b 34  b 44  b 54

a 15 a 25 a 35 a 45 a 55

 b 15 º  b 25 »»  b 35 » »  b 45 »  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 « 21 «c 31 « «c 41 «¬c 51

c 12 c 22 c 32 c 42 c 52

c 13 c 23 c 33 c 43 c 53

c 14 c 24 c 34 c 44 c 54

c 15 º c 25 »» c 35 » “ » c 45 » c 55 »¼

ªa 11 «a « 21  «a 31 « «a 41 «¬a 51

a 12 a 22 a 32 a 42 a 52

a 13 a 23 a 33 a 43 a 53

a 14 a 24 a 34 a 44 a 54



 

a 15 º ªb 11 «b » a 25 » « 21 a 35 » b «b 31 « » a 45 » «b 41 «¬b 51 a 55 »¼

b 12 b 22 b 32 b 42 b 52

b 13 b 23 b 33 b 43 b 53

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

b 14 b 24 b 34 b 44 b 54

b 15 º b 25 »» b 35 »  » b 45 » b 55 »¼

 .G52&0'IA1C/60D(A1RE 
!:(<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

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
mulMatrices(int[][]

m1,

int[][]

m2)

%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]; [][]c = new int[5][5];  int for (int i=0; i


 

 !"!#$%&'(&" )&*'+," !"!#$%-(./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' rel="nofollow">'?'.'@'/"3n'D9420%1'(&(2'6/($E&/60ADA %B0%C"'5C/6/#,0"E9"
 

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

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

9%B I29"1'((%$6H'C2/0M#&0'IA1C/6/($E.'1"2/$G:0'1

%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,+
 

 !"!#$%&'(&" )&*'+," !"!#$%-(./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
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
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
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
 

 !"!#$%&'(&" )&*'+," !"!#$%-(./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
("'  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 
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"     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"    &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

    

%BI7B/

%B&H6WH'"0':;"

    





 #'&&=',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/  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
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" 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

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' 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
Arrays

0%0L/5&=',n %B 1%$B @C2/61,+/($E7/ binarySearch()<#fill() 

sort(),

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

<# 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

 

Collection,

Set,

List

 !"!#$%&'(&" )&*'+," !"!#$%-(./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,"
    

    





           

 

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

Related Documents

Chap 8
November 2019 14
Chap 8
December 2019 11
Chap 8
November 2019 17
Chap 8
November 2019 16
Chap 8
June 2020 12
Chap 8
November 2019 15