Chap 8
Chap 8
8
+"#
##*')
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
>'?'.'@'<+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"
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
variableName[index]
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
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]);
}
}
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
}
4!,W##,-LE G%B 52.'11'((,"!:(<1(0 %BzQc
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()+" ");
}
}
}
!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"
ª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 »¼
("' 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:");
4!4 W##,-LE %BG52.'11'((,"!:(<1(0 %BzQ
("' 4!3D,@/$H'61'((%$19I20L/5C/6#'&Arrays
import java.util.Arrays;
("' 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 wC2/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
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 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)
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
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"
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
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()
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
/&
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()