3. ARRAY
Mr.Warawut
Khangkhan
Array ( F F) ˈ F F ก F
ก F กก F 1 F F ก F
Chapter 5 Array & String
ก F (Index) ˈ ก
Array & String
ก
F F F ก F F F ก F
F F ก F Index F F F
F ก
Index [0] [1] … [n-1]
3
ก 1 ก 2 … ก n
4. F F
Mr.Warawut
Khangkhan
กF F ก
F F F
Chapter 5 Array & String
(Array of Primitive Data
Type)
Array & String
F F F F F (Array of
Reference Data Type)
กF F F
F F1 (One-Dimensional Array)
F F (Multi-Dimensional Array)
4
5. ONE-DIMENSIONAL ARRAY
Mr.Warawut
Khangkhan
ก ก F F 1
Chapter 5 Array & String
dataType [ ] arrayName
ก F F F ก F ก
Array & String
arrayName = new dataType[ n ];
Example:
int[ ] number;
number = new int[5];
or 5
int number = new int[5];
6. ONE-DIMENSIONAL ARRAY
Mr.Warawut
Khangkhan
ก ก F F F F
Chapter 5 Array & String
dataType[ ] arrayName =
{ init_value1, init_value2, …,
Array & String
init_value };
Example:
char[ ] grade = {‘A’, ‘B’, ‘C’, ‘D’, ‘F’};
6
10. ONE-DIMENSIONAL ARRAY
Mr.Warawut
Khangkhan
ก F ก F F F F F for
Chapter 5 Array & String
for (int i = 0; i < arrayName.length; i++) {
statements;
Array & String
};
length ˈ method F ก
F F
10
12. METHOD CLASS ARRAYS
Mr.Warawut
Khangkhan
method binarySearch( ) – F ก
ก array
Chapter 5 Array & String
arrayName.length;
n = arrayName.length;
Array & String
method sort( ) – F
array ก F ก
Arrays.sort(arrayName);
Arrays.sort(arrayName);
12
13. METHOD CLASS ARRAYS
Mr.Warawut
Khangkhan
method binarySearch( ) – F ก F
F F ก array ( กF )
Chapter 5 Array & String
idxValue =
Array & String
Arrays.binarySeach(arrName,
Arrays.binarySeach(arrName,
val);
val);
method fill( ) – F ก F F Fก
array ก F ก
Arrays.fill(arrayName,
Arrays.fill(arrayName, value);
13
14. METHOD CLASS ARRAYS
Mr.Warawut
Khangkhan
method equals( ) – F F
F array
Chapter 5 Array & String
result =
Array & String
Arrays.equals(arrayName
(arrayName1
Arrays.equals(arrayName1,
arrayName2
arrayName2);
Ch05
05_
Example Source Code: Ch05_04
14
15. TWO-DIMENSIONAL ARRAY
Mr.Warawut
Khangkhan
ก ก F F 2
Chapter 5 Array & String
dataType [ ][ ] arrayName
ก F F F ก F ก
Array & String
arrayName = new dataType[ m ][ n ];
Example:
int[ ][ ] number;
number = new int[2][4];
15
17. F F ARRAYLIST
Mr.Warawut
Khangkhan
ArrayList ˈ F F ก ก
ก F F F F F F
Chapter 5 Array & String
F ก F (index) F
Array & String
F ก
ArrayList ก ก F object ˈ
array Reference
F
17
18. METHOD CLASS
ARRAYLIST
Mr.Warawut
Khangkhan
method size( ) – ก
ArrayList
Chapter 5 Array & String
arrayListName.size(
n = arrayListName.size( );
Array & String
method add( ) – F ก ArrayList
arrayListName.add(objectValue);
arrayListName.add(objectValue);
or
arrayListName.add(index,
arrayListName.add(index,
objectValue);
objectValue);
18
19. METHOD CLASS
ARRAYLIST
Mr.Warawut
Khangkhan
method get( ) – F ก ArrayList
F F ก
Chapter 5 Array & String
objectValue =
arrayListName.get(index);
arrayListName.get(index);
Array & String
method remove( ) – F ก ArrayList
ArrayList F F ก
arrayListName.remove(index);
arrayListName.remove(index);
method indexOf( ) – F F F F ก
F ก ArrayList
index =
arrrayListName.indexOf(objectValue);
arrrayListName.indexOf(objectValue); 19
21. ก F FOR EACH LOOP
Mr.Warawut
Khangkhan
ˈ F F F F F F
ArrayList
Chapter 5 Array & String
F ˂ ก ก ก F ก F F ก
Array & String
F F
(IndexOutOfBoundsException)
F ก F ก ก
F ก F
21
22. ก F FOR EACH LOOP
Mr.Warawut
Khangkhan
Chapter 5 Array & String
for (arrayType arrayValue : arrayName) {
statement;
Array & String
}
arrayType ˈ F array ArrayList
arrayValue ˈ F F F F
arrayName ˈ array ArrayLit
22
24. ก F ENUMERATED TYPES
Mr.Warawut
Khangkhan
ˈ F F F ก กF F
F F ก ˈ F F
Chapter 5 Array & String
Array & String
enumName {value-1, value-2, …, value-n}
Ch05
05_
Example Source Code: Ch05_08
24
28. METHOD CLASS STRING
Mr.Warawut
Khangkhan
method equals( ) – F F
F String 2 F
Chapter 5 Array & String
str1.equals(str2
result = str1.equals(str2);
Array & String
F F Fก F ˈ True
F F F Fก F ˈ False
28
30. METHOD CLASS STRING
Mr.Warawut
Khangkhan
method compareTo( ) – F
F F String 2 F
Chapter 5 Array & String
str1.compareTo(str2
result = str1.compareTo(str2);
Array & String
F str1 F > str2 F ˈ ก
F str1 F = str2 F ˈ F
F str1 F < str2 F ˈ
30
36. METHOD CLASS STRING
Mr.Warawut
Khangkhan
method replace( ) – F F
String F ˈ F F
Chapter 5 Array & String
str2 str1.replace(str3 str4
str2 = str1.replace(str3, str4);
Array & String
str3 ˈ F ก F
str4 ˈ F F
36
38. METHOD CLASS STRING
Mr.Warawut
Khangkhan
method toUpperCase( ) – F
F String F ˈ ก F F
Chapter 5 Array & String
str2 str1
str2 = str1.toUpperCase( );
Array & String
method toLowerCase( ) – F
F String F ˈ ก F ก
str2 str1
str2 = str1.toLowerCase( );
method length( ) – F ก
F String
n = str.length( );
str.length(
38
40. METHOD CLASS STRING
Mr.Warawut
Khangkhan
method charAt( ) – F F
F ก F String F F ก
Chapter 5 Array & String
str.charAt(index);
ch = str.charAt(index);
Array & String
method indexOf( ) – F F
F F ก F String
index = str.indexOf(ch);
str.indexOf(ch);
40
42. METHOD CLASS STRING
Mr.Warawut
Khangkhan
method startsWith( ) – F
F F F String
Chapter 5 Array & String
str1.startsWith(str2
result = str1.startsWith(str2);
Array & String
Method endsWith( ) – F
F F F F String
str1.endsWith(str2
result = str1.endsWith(str2);
result F ˈ True False 42
44. METHOD CLASS STRING
Mr.Warawut
Khangkhan
method lastIndexOf( ) – F F
F F F ก F
Chapter 5 Array & String
String
Array & String
index = str.lastIndexOf(ch);
str.lastIndexOf(ch);
index ˈ F F ก ch
F str ก F ก ch
F F ˈ
44
46. STRINGBUFFER
STRINGBUILDER
Mr.Warawut
Khangkhan
class StringBuffer ก ก F
String F F กF
Chapter 5 Array & String
ก String
Array & String
String F F F F ก
ก F F
ก F method F String ˈก
F String F (ก กก F )
F StringBuffer String 46
47. STRINGBUFFER
STRINGBUILDER
Mr.Warawut
Khangkhan
class StringBuilder ก ก F
StringBuffer F
Chapter 5 Array & String
F กF ก StringBuffer
Array & String
class StringBuffer ก
F ก F Thread
Synchronization F F F ก F class
StringBuilder
47
48. METHOD CLASS STRINGBUFFER
AND STRINGBUILDER
Mr.Warawut
Khangkhan
method append( ) F F
F F F StringBuffer
Chapter 5 Array & String
str3
str3 = str1.append(str2);
str1.append(str2
Array & String
method insert( ) F F
ก F F StringBuffer F
F ก
str3
str3 = str1.insert(index, str2);
str1 str2
48
49. METHOD CLASS STRINGBUFFER
AND STRINGBUILDER
Mr.Warawut
Khangkhan
method delete( ) F F
F F StringBuffer F
Chapter 5 Array & String
F ก
Array & String
str2 = str1.delete(x, y);
str2 str1
method length( ) F
ก F StringBuffer
int n = str.length( );
str.length(
49