1
ARRAYS & METHODS
Array ArrayList Class Method Method Overloading Parameter Passing
Watcharin Puangplia : 5/12/53
What’s an Array 2
Array ?
ďœ’ ďœ‹ ďœ‹ ďœ‹ ďœ‹ ďœ‹ ďœŠ "# ďœ’ ďœ‹ # $ ďœ‹ "# % ŕš‰ % ďœ’ Primitive type 0 Object reference ďœ‹ 7 8 Java ŕš‰ Array Object ( ďœ’ instance class Array)
ďœ‹ "# ďœŠ Array %@ # $ďœŠ Array
Array 0 % $ Array ŕš‰ ďœ‹ A Array A" ďœ‹ @ B "# Array # "# ďœŠ $$ďœŠ index Aďœ‹ Array # ďœŠ "ďœŠ N F ďœ‹$ index Array ďœŠ $%@ # ďœŠ G ŕš‰ FGďœŠ 0 A N-1
What’s an Array 3
Scores Reference name of Array
index Array
0
1
2
3
4
5
6
58
76
49
83
66
77
61
()*+,- Array
Array ďœŠ integer 7 ďœŠ ŕš‰ Array = 7 index Array # ďœŠ G ŕš‰ FGďœŠ 0 A 6
Last index
First index
Array ?
- - Array 4
Declaration & Creating & Initialization Syntax -*5657-*89+ď&#x153;&#x2039; Array (*)*5;657-*8<=ď&#x153;&#x2039; => ?@A <type> [] <ref name>; <ref name> = new <type> [<size>]; 0 <type> [] <ref name> = new <type> [<size>]; 0 <type> [] <ref name> = {<value 1>..<value n>};
- - Array 5
Declaration & Creating & Initialization
Scores
0
1
2
3
4
5
6
58
76
49
83
66
77
61
<type> [] <ref name> = new <type> [<size>]; int
[]
scores
= new
int
[7];
scores[0] = 58; scores[1]=76; â&#x20AC;Ś, scores[6]= 61; int
[]
scores
= {58,76,49,83,66,77,61};
Length of Array 6
Scores
0
1
2
3
4
5
6
58
76
49
83
66
77
61
ď&#x153;&#x2039; Array Gď&#x153;&#x2039; # @ B Array "B ŕš&#x2030; Object Array "# A ď&#x153;&#x2039; ŕš&#x2030; "B Object %@ # ď&#x153;&#x160; "# (public constant) # $ď&#x153;&#x160; â&#x20AC;&#x2DC;lengthâ&#x20AC;&#x2122; "# ď&#x153;&#x160; Array % G . x = scores.length; // x value is 7 * GB$ď&#x153;&#x160; â&#x20AC;&#x2DC;lengthâ&#x20AC;&#x2122; %@ % $ " ŕš&#x2030; 0
Array ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x160; index B "ď&#x153;&#x2039; (last index)
Array 7
Reference : -*5 ď&#x153;&#x2039;* ,
Scores
0
1
2
3
4
5
6
58
76
49
83
66
77
61
Aď&#x153;&#x2039; Array # ď&#x153;&#x160; "ď&#x153;&#x160; N F ď&#x153;&#x2039;$ A ď&#x153;&#x2039; 0 ď&#x153;&#x2039; A ď&#x153;&#x160; Gď&#x153;&#x160; "# ď&#x153;&#x160; Array ď&#x153;&#x2039; ď&#x153;&#x2039; 0 Array F ď&#x153;&#x2039;$G ď&#x153;&#x2039;$ index "# Gď&#x153;&#x2039; ď&#x153;&#x2039; g index ď&#x153;&#x160; $ Gď&#x153;&#x2039; # ď&#x153;&#x160; 0 â&#x20AC;&#x2DC;[â&#x20AC;&#x2122; F @ â&#x20AC;&#x2DC;]â&#x20AC;&#x2122; F @ # ď&#x153;&#x160; ď&#x153;&#x160; @ $ď&#x153;&#x160; 0 A N-1 x = scores[5]; // x ď&#x153;&#x160; 77 scores[5] = 70; // ď&#x153;&#x160; index "# 5 # % 77 ď&#x153;&#x2019; 70
Arrays Objects 8
Words
0
1
2
3
4
5
6
A
B
C
D
E
F
G
Array A ď&#x153;&#x2019; Object ref ď&#x153;&#x2039; ď&#x153;&#x160; # $
ď&#x153;&#x2039; "# ď&#x153;&#x2019; Primitive type 8h@ @ i ď&#x153;&#x2039; String [] words = new String [7]; % Statement G $ ď&#x153;&#x160; ď&#x153;&#x2019; ď&#x153;&#x2039; Object ref Array words FGď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x2039; Object String
Copying Arrays 9
-*5 >=G - Arrays
ŕš&#x2030; F "# ď&#x153;&#x2039; ŕš&#x2030; %Gď&#x153;&#x2039; Array 0 ď&#x153;&#x160;$ Array F ď&#x153;&#x2039; ď&#x153;?l ď&#x153;&#x160; $ A " ď&#x153;&#x2039; #ŕš&#x2030; - ď&#x153;&#x2039; Pointer 0 Reference (Copy pointer) ď&#x153;&#x2039;$ ď&#x153;&#x2039; 0 (=) o0 # ď&#x153;&#x2039; Object % Object ď&#x153;&#x2019; Object 0 - ď&#x153;&#x2039; ď&#x153;&#x160; (Copy value) % ď&#x153;&#x160; Array ď&#x153;&#x2039; Array 0
Copying Arrays by copy pointer 10
Before
-*5 >=G - Arrays =ď&#x153;&#x2039;HIJ 5 K L)*I =
list1 list 1
A
B
C
D
E
F
G
list2 list 2
H
I
J
K
L
M
N
After
list2 list 2 = list1 list1 list1 list 1
A
B
C
D
E
F
G
H
I
J
K
L
M
N
list2 list 2 Garbage
Copying Arrays by copy value 11
-*5 >=G - Arrays =ď&#x153;&#x2039;HI-*59+ď&#x153;&#x2039; Loop
source
1
3
5
7
9
target int [] source = {1, 3, 5, 7, 9, 11, 13}; int [] target = new int [source.length]; ď&#x153;&#x160; Gď&#x153;&#x2039;
0
o ď&#x153;&#x160; / ď&#x153;&#x160;
for (int i= 0; i < source.length; i++) target[i] = source[i];
11
13
Copying Arrays by copy value 12
-*5 >=G - Arrays =ď&#x153;&#x2039;HI-*59+ď&#x153;&#x2039; arraycopy
source
1
3
5
7
9
11
13
target ď&#x153;&#x2039; "q arraycopy # Sysntax ď&#x153;&#x2039; #ŕš&#x2030; System.arraycopy (srcArr, start_pos_src, targetArr, start_pos_taget, length) int [] source = {1, 3, 5, 7, 9, 11, 13}; int [] target = new int [source.length]; System.arraycopy (source, 0, target, 0, source.length)
Copying Arrays by copy value 13
int elements [ ] = { 1,2,3,4 }; // original array int hold [ ] = { 10,9,8,7,6,5 }; // new larger array System.arraycopy(elements, 0, hold, 0, elements.length); Origin
Target
Length of copy
Multidimensional Arrays 14
Array UVVLG*I),W, 7 8 Java A ď&#x153;&#x2039; Array "# # $ď&#x153;&#x160; G ď&#x153;&#x2039; o â&#x20AC;&#x2DC;[â&#x20AC;&#x2122; F @ â&#x20AC;&#x2DC;]â&#x20AC;&#x2122; ď&#x153;&#x2039; FGď&#x153;&#x160; @ G
int int int int
[] a = new int [3]; [][] a2D = new int [3][4]; [][][] a3D = new int [3][4][2]; []â&#x20AC;Ś[] aND = new int [n]â&#x20AC;Ś[n];
Array 2 15
int[][] a2D = new int[2][3]; a2D[0][0] = 1 a2D[0][1] = 2 a2D[0][2] = 3 R0 a2D[1][0] = 4 a2D[1][1] = 5 R1 a2D[1][2] = 6
C0
C1
C2
1
2
3
4
5
6
int[][] a2D = { {1, 2, 3}, {4, 5, 6} };
Array 3 16
int[][][] a3D = new int[2][3][2]; a2D[0][0][0] = 1 a2D[1][0][0] = 7 a2D[0][0][1] = 2 a2D[1][0][1] = 8 a2D[0][1][0] = 3 a2D[1][1][0] = 9 0 a2D[0][1][1] = 4 a2D[1][1][1] = 10 1 a2D[0][2][0] = 5 a2D[1][2][0] = 11 a2D[0][2][1] = 6 a2D[1][2][1] = 12 int[][] a2D = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } };
C0 C1 R0
1
2
R1
3
4
R2
5
6
C0 C1 R0
7
8
R1
9
10
R2
11
12
Multidimensional Arrays 17
One dimension
row
Two dimension [3][4 ][4]
Three dimension [3][4 ][4][2 ][2]
column
The ArrayList Class 18
ď&#x153;&#x2019; ď&#x153;&#x160;$ package java.util # 8h@ ď&#x153;&#x2039; Array 0 ď&#x153;&#x2019; ď&#x153;&#x2039; ď&#x153;&#x2039; "# ď&#x153;&#x2039;
ď&#x153;&#x2039; ď&#x153;&#x2039; ď&#x153;&#x160; F @ ď&#x153;&#x2039; A ď&#x153;&#x2039; ď&#x153;&#x2039; index FG Gď&#x153;&#x160; % Array G "# ArrayList ď&#x153;&#x160;Gď&#x153;&#x2039; ď&#x153;&#x2039; F @ ď&#x153;&#x2039;G Gď&#x153;&#x2039; # Methods Gď&#x153;&#x160; "# ď&#x153;&#x160; Gď&#x153;&#x160; " ď&#x153;&#x2039; ď&#x153;&#x2039; # ď&#x153;&#x2039;
18
- - ArrayList 19
Declaration & Creating & Initialization Syntax @ i ď&#x153;&#x2039; ArrayList A @ i ď&#x153;&#x2039; #ŕš&#x2030; ArrayList<type> <ref name>; <ref name> = new ArrayList<type>(); 0 ArrayList<type> <ref name> = new ArrayList<type>();
- - ArrayList 20
Declaration & Creating & Initialization Syntax @ i ď&#x153;&#x2039; ArrayList A @ i ď&#x153;&#x2039; #ŕš&#x2030; ArrayList <type> <ref name> = new ArrayList <type>(); ArrayList <Integer> numList numList.add(new Integer(1)); numList.add(new Integer(2)); â&#x20AC;Ś numList.add(new Integer(n));
= new ArrayList <Integer>();
Method of ArrayList 21
Method ArrayList add(object o) : boolean add(int index, object o) : void o Object ď&#x153;&#x2039; ď&#x153;&#x160; ArrayList remove(object o) : boolean remove(int index) : object Object % ArrayList size() : int 0 ď&#x153;&#x160; % $ ArrayList get(int index) : object 0 ď&#x153;&#x160; Object G index "# @ B
Method of ArrayList 22
Method ArrayList clear() : void " ŕš&#x2030; ArrayList contains(object o) : boolean 0 ď&#x153;&#x160; true Aď&#x153;&#x2039; Object "# @ B # ď&#x153;&#x160; ArrayList isEmpty() : boolean 0 ď&#x153;&#x160; true 0 ArrayList ď&#x153;&#x160; # ď&#x153;&#x160;
Method of Array & ArrayList 23
Method Array UG7 ArrayList Array
List
array.length
ď&#x153;&#x2039; A List array[index]
ArrayList arrayList.size() arrayList.get(index)
o List
array[index] = * arrayList.add(object)
List
array[index] = * arrayList.remove(object)
Excemple : Method of Array & ArrayList
24
Excemple : Method of Array & ArrayList !" -- Example for Get Value from ArrayList -Get ArrayList value: value1 Remove ArrayList value: value3 Contains ArrayList: true Contains ArrayList: false
25
Excemple : Array
10 (Array) %& ' ( )
26
Lab Assignment
Problem 1 : AssignArray1.java
! " #$ % & 10 '( & ) * + (!, -./ .0 1( 2 " ! ) * + Enter score [1] = ??? Enter score [2] = ??? Enter score [3] = ??? Enter score [4] = ??? Enter score [5] = ??? Enter score [6] = ??? Enter score [7] = ??? Enter score [8] = ??? Enter score [9] = ??? Enter score [10] = ??? Summary = ??? Average = ??? 27
Lab Assignment
Problem 1 : AssignArray1.java
! " #$ % & 10 (! ! 3 4+ Enter score [1] = ??? Enter score [2] = ??? Enter score [3] = ??? Enter score [4] = ??? Enter score [5] = ??? Enter score [6] = ??? Enter score [7] = ??? Enter score [8] = ??? Enter score [9] = ??? Enter score [10] = ??? Maximum value is = ??? 28