SlideShare una empresa de Scribd logo
1 de 50
Descargar para leer sin conexión
CHAPTER 5
ARRAY & STRING
Mr.Warawut Khangkhan
e-Mail: awarawut@hotmail.com
Social Media: www.facebook.com/AjWarawut
Mr.Warawut Khangkhan   Chapter 5 Array & String
                                 Array & String
                                            ARRAY
                                                    2
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
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
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];
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
ONE-DIMENSIONAL ARRAY




                              Mr.Warawut
                               Khangkhan
ก ก    F F       F F( F)




                                  Chapter 5 Array & String
arrayName[0] = init_value1;
arrayName[1] = init_value2;




                                            Array & String
…
arrayName[n-1] = init_value

Example:
   char[ ] grade;
   grade = new char[2];
   grade[0] = ‘A’             7

   grade[1] = ‘B’
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        8
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        9
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        11
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
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
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
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        16
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
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
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        20
ก           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
ก    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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        23
ก       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
Mr.Warawut Khangkhan   Chapter 5 Array & String
                                 Array & String
                                            STRING
                                                     25
Mr.Warawut
                  Chapter 5 Array & String
                            Array & String
      Khangkhan
                                             26
         F


                               F
                        ก F
                        F FF
                        ˈก
                        F F
         ก


                               F
         F


                               F ก
         ก
STRING

                        F
                      F F
         F
                  ก ˈ F
         ˈ

                  ก
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        27
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        29
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        31
METHOD        CLASS      STRING




                                  Mr.Warawut
                                   Khangkhan
method concat( ) – F        F F
  String    F      +




                                    Chapter 5 Array & String
  str3   str1.concat(str2
  str3 = str1.concat(str2);




                                              Array & String
   or
   str3
   str3 = str1 + str2;
          str1   str2



                                  32
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        33
METHOD        CLASS     STRING




                                   Mr.Warawut
                                    Khangkhan
method substring( ) – F        F
  String F ˈ F   F




                                     Chapter 5 Array & String
  str2   str1
  str2 = str1.substring(x, y);




                                               Array & String
                                   34
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        35
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        37
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        39
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        41
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        43
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        45
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
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
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
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
Mr.Warawut
             Chapter 5 Array & String
                       Array & String
 Khangkhan
                                        50

Más contenido relacionado

La actualidad más candente

La actualidad más candente (6)

AI Lesson 15
AI Lesson 15AI Lesson 15
AI Lesson 15
 
ON SEMI-  -CONTINUITY WHERE   {L, M, R, S}
ON SEMI-  -CONTINUITY WHERE   {L, M, R, S}ON SEMI-  -CONTINUITY WHERE   {L, M, R, S}
ON SEMI-  -CONTINUITY WHERE   {L, M, R, S}
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
 
L0342067075
L0342067075L0342067075
L0342067075
 
AI Lesson 14
AI Lesson 14AI Lesson 14
AI Lesson 14
 
www.ijerd.com
www.ijerd.comwww.ijerd.com
www.ijerd.com
 

Destacado (11)

Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
การใช้ตัวแปรอาร์เรย์ (Array) ใน VB.NET 2005 Express Editor
การใช้ตัวแปรอาร์เรย์ (Array) ใน VB.NET 2005 Express Editorการใช้ตัวแปรอาร์เรย์ (Array) ใน VB.NET 2005 Express Editor
การใช้ตัวแปรอาร์เรย์ (Array) ใน VB.NET 2005 Express Editor
 
Database design
Database designDatabase design
Database design
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 

Más de Warawut

Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
Warawut
 
Object-Oriented Programming 1
Object-Oriented Programming 1Object-Oriented Programming 1
Object-Oriented Programming 1
Warawut
 

Más de Warawut (20)

Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 
Object-Oriented Programming 1
Object-Oriented Programming 1Object-Oriented Programming 1
Object-Oriented Programming 1
 
Upload File
Upload FileUpload File
Upload File
 
Login
LoginLogin
Login
 
Session and Cookie
Session and CookieSession and Cookie
Session and Cookie
 
Form Validation
Form ValidationForm Validation
Form Validation
 
Tips & Track
Tips & TrackTips & Track
Tips & Track
 
Edit & Delete Data
Edit & Delete DataEdit & Delete Data
Edit & Delete Data
 
Search Data
Search DataSearch Data
Search Data
 
Retrieve Data
Retrieve DataRetrieve Data
Retrieve Data
 
Additional Information
Additional InformationAdditional Information
Additional Information
 
Connect MySQL
Connect MySQLConnect MySQL
Connect MySQL
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 

Object-Oriented Programming 5

  • 1. CHAPTER 5 ARRAY & STRING Mr.Warawut Khangkhan e-Mail: awarawut@hotmail.com Social Media: www.facebook.com/AjWarawut
  • 2. Mr.Warawut Khangkhan Chapter 5 Array & String Array & String ARRAY 2
  • 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
  • 7. ONE-DIMENSIONAL ARRAY Mr.Warawut Khangkhan ก ก F F F F( F) Chapter 5 Array & String arrayName[0] = init_value1; arrayName[1] = init_value2; Array & String … arrayName[n-1] = init_value Example: char[ ] grade; grade = new char[2]; grade[0] = ‘A’ 7 grade[1] = ‘B’
  • 8. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 8
  • 9. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 9
  • 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
  • 11. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 11
  • 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
  • 16. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 16
  • 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
  • 20. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 20
  • 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
  • 23. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 23
  • 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
  • 25. Mr.Warawut Khangkhan Chapter 5 Array & String Array & String STRING 25
  • 26. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 26 F F ก F F FF ˈก F F ก F F F ก ก STRING F F F F ก ˈ F ˈ ก
  • 27. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 27
  • 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
  • 29. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 29
  • 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
  • 31. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 31
  • 32. METHOD CLASS STRING Mr.Warawut Khangkhan method concat( ) – F F F String F + Chapter 5 Array & String str3 str1.concat(str2 str3 = str1.concat(str2); Array & String or str3 str3 = str1 + str2; str1 str2 32
  • 33. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 33
  • 34. METHOD CLASS STRING Mr.Warawut Khangkhan method substring( ) – F F String F ˈ F F Chapter 5 Array & String str2 str1 str2 = str1.substring(x, y); Array & String 34
  • 35. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 35
  • 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
  • 37. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 37
  • 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
  • 39. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 39
  • 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
  • 41. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 41
  • 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
  • 43. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 43
  • 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
  • 45. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 45
  • 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
  • 50. Mr.Warawut Chapter 5 Array & String Array & String Khangkhan 50