งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Java Array and String Jarungjit Parnjai

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Java Array and String Jarungjit Parnjai"— ใบสำเนางานนำเสนอ:

1 Java Array and String Jarungjit Parnjai
Object Oriented Technology Java Array and String Jarungjit Parnjai

2 Java Array

3 Java Array แอร์เรย์ (Array)
หมายถึง กลุ่ม (group) ของข้อมูลที่เป็นประเภทเดียวกัน Array ในภาษาจาวา ไม่ใช่เป็นเพียงเนื้อที่ในหน่วยความจำที่จองไว้สำหรับประเภทข้อมูล แต่เป็นวัตถุ (Object) เป็นกลุ่ม (group) ของข้อมูลที่เป็นประเภทเดียวกัน (ทั้งประเภท Primitive Data Type และ Reference Type)

4 Declare A as an Array of Character
Examples of Java Array Element Index Length=3 [0] [1] [2] xxx ‘a’ ‘b’ ‘c’ yyy Memory Address zzz Memory Address aaa Declare A as an Array of Character A

5 Declaring Array in Java
type[ ] variable; หรือ type variable[ ]; ประกาศตัวแปร Array เพื่อเป็นตัวอ้างอิงไปยัง Array เป็นการจองเนื้อที่สำหรับตัวแปรอ้างอิง (Reference Variable) ไม่ระบุขนาดของ Array ค่า Default value เป็น null Array เป็น Object???

6 Examples of Declaring Array
int[ ] i; หรือ int i[ ]; int[ ] i,j; หรือ int i[ ], j[ ]; double[ ] d; หรือ double d[ ]; char[ ] A; หรือ char A[ ]; BankAccount[ ] ba; หรือ BankAccount ba[ ]; Point[ ] pt; หรือ Point pt[ ];

7 Creating Array in Java การสร้าง Array ในภาษาจาวา ทำได้โดย
ใช้ new ในการสร้าง Array เป็นการจองเนื้อที่สำหรับเก็บข้อมูลใน Array มีการระบุขนาดของเนื้อที่ที่ต้องการสร้าง ตัวอย่างการสร้าง Array char c[ ] = new char[26]; int i[ ] = new int[100]; double flag[ ] = new double[10]; BankAccount ba[ ] = new BankAccount[20]; Person p[ ] = new People[200];

8 Array Referencing เมื่อ Array ถูกสร้างขึ้น ขนาดของ Array จะมีค่าคงที่ ไม่สามารถเปลี่ยนแปลงได้ Index ของ Array จะต้องมีค่า >= 0 <= .length - 1 การตรวจสอบ ขอบเขตของ Array (Array Bounds) JVM ทำให้อัตโนมัติ ตรวจสอบ index ของ Array, ต้องเป็น 0 <= index <= .length-1 Throws Exception ที่ว่า “Array Index Out of Bounds”

9 Declare C as an Array of Character
Creating Array in Java ตัวอย่างการสร้าง Array ของ Primitive Data Type char[ ] c; c = new char[26]; for (int i=0; i<26; i++) { 4 c[i] = (char)('a'+i); } YYY Memory Address XXX Declare C as an Array of Character c After Line 1 null

10 Creating Array in Java ตัวอย่างการสร้าง Array ของ Primitive Data Type
char[ ] c; c = new char[26]; for (int i=0; i<26; i++) { 4 c[i] = (char)('a'+i); } aaa Memory Address XXX C Element Index Length=26 [0] [1] [2] YYY bbb zzz ccc [25] After Line 2 null Declare C as an Array of Character YYY Memory Address XXX C Executing Line 2 null Declare C as an Array of Character aaa Memory Address XXX C Element Index Length=26 [0] [1] [2] YYY bbb zzz ccc [25] Executing Line 2 null Declare C as an Array of Character

11 Creating Array in Java ตัวอย่างการสร้าง Array ของ Primitive Data Typev
char[ ] c; c = new char[26]; for (int i=0; i<26; i++) { 4 c[i] = (char)('a'+i); } aaa Memory Address XXX C Element Index Length=26 [0] [1] [2] ‘b’ bbb zzz ccc [25] After Line 3 and 4 null Declare C as an Array of Character YYY ‘z’ ‘c’ ‘a’ Memory Address aaa XXX C Element Index Length=26 [0] [1] [2] YYY bbb zzz ccc [25] Executing Line 3 and 4 null Declare C as an Array of Character ‘a’

12 Declare C as an Array of Character
Creating Array in Java ตัวอย่างการสร้าง Array ของ Primitive Data Type char[ ] c; c = new char[26]; for (int i=0; i<26; i++) { 4 c[i] = (char)('a'+i); } aaa Memory Address XXX C Element Index Length=26 [0] [1] [2] ‘b’ bbb zzz ccc [25] After Line 5 null Declare C as an Array of Character YYY ‘z’ ‘c’ ‘a’

13 Initializing an Array การสร้าง Array โดยให้ค่าเริ่มต้น
BankAccount account[ ]; account = new BankAccount[3]; account[0] = new BankAccount(1000); account[1] = new BankAccount(0); account[2] = new BankAccount (23052); int data[ ]; data = new int[5]; for (int i=10; I < data.length; i--) data[i] = i % 2; Array Bounds int a[ ] = { 2, 4, 6, 8, 10 };

14 A Point Class xPos yPos Point Point - xPos : int - yPos : int
+ Point(initX : int, initY: int) + getX( ) : int + getY( ) : int + setPostion(X : int, Y: int) Class Diagram Point setPostion(x, y) getX() yPos xPos Object Diagram getY()

15 Declare pt as an Array of Point Object
Creating Array in Java ตัวอย่างการสร้าง Array ของ Primitive Data Type Point[ ] pt; pt = new Point[10]; for (int i=0; i<pt.length; i++) { 4 pt[i] = (Point)('a'+i); } After Line 1 pt XXX YYY Memory Address null YYY Declare pt as an Array of Point Object

16 Creating Array in Java ตัวอย่างการสร้าง Array ของ Primitive Data Type
Point[ ] pt; pt = new Point[10]; for (int i=0; i<pt.length; i++) { 4 pt[i] = new Point(i, i+1); } aaa Memory Address XXX pt Element Index Length=10 [0] [1] [2] YYY bbb zzz ccc [9] After Line 2 null Declare pt as an Array of Character aaa Memory Address XXX pt Element Index Length=10 [0] [1] [2] YYY bbb zzz ccc [9] Executing Line 2 null Declare pt as an Array of Character

17 Creating Array in Java ตัวอย่างการสร้าง Array ของ Primitive Data Type
Point[ ] pt; pt = new Point[10]; for (int i=0; i<pt.length; i++) { 4 pt[i] = new Point(i, i+1); } aaa Memory Address XXX pt Element Index Length = 10 [0] [1] [2] (1,2) bbb zzz ccc [9] After Line 5 null Declare pt as an Array of Character YYY (9,10) (2,3) (0,1) 1 X Y Point aaa Memory Address XXX pt Element Index Length = 10 [0] [1] [2] (1,2) bbb zzz ccc [9] After Line 5 null Declare pt as an Array of Character YYY (9,10) (2,3) (0,1)

18 A Line class ตัวอย่างการสร้าง Array ของคลาส Line Line
- startPt : Point - endPt : Point + Line(start :Point, start : Point) + getStartPoint ( ) : Point + getEndPoint( ) : Point + setStartPoint (pt :Point) + setEndPoint (pt :Point) Class Diagram Line[ ] line; line = new Line[3]; for (int i=0; i<line.length; i++) { Point pt1 = new Point(i); Point pt2 = new Point(i+1); 6 line[i] = new Line(pt1,pt2); }

19 Array Resizing in Java ในภาษาจาวา ไม่สามารถ resize Array ได้
ตัวอย่าง int myArray [ ] = new int[4]; myArray = new int[6]; jjj XXX myArray null aaa bbb ddd ccc [0] [1] [2] [3] After Line 2 kkk nnn mmm ooo ppp [4] [5] int myArray [ ] = new int[4]; myArray = new int[6]; aaa XXX myArray null bbb ddd ccc [0] [1] [2] [3] After Line 1 int myArray [ ] = new int[4]; myArray = new int[6]; aaa XXX myArray null bbb ddd ccc [0] [1] [2] [3] Executing Line 2 jjj kkk nnn mmm ooo ppp [4] [5]

20 Copying Array in Java ในภาษาจาวา ทำได้โดย
เรียกใช้เมทธอด System.arrraycopy( ); ตัวอย่าง Origin Target 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); Length of copy aaa XXX elements [0] [1] [2] 2 bbb ddd ccc [3] After Line 2 4 3 1 jjj hold 9 kkk nnn mmm 8 10 7 6 5 ooo ppp [4] [5]

21 Copying Array in Java ตัวอย่าง 2 4 3 1 6 5 2 4 3 1 9 8 10 7 6 5
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); aaa XXX elements [0] [1] [2] 2 bbb ddd ccc [3] After Line 3 4 3 1 jjj hold kkk nnn mmm 6 5 ooo ppp [4] [5] aaa XXX elements [0] [1] [2] 2 bbb ddd ccc [3] Executing Line 3 4 3 1 jjj hold 9 kkk nnn mmm 8 10 7 6 5 ooo ppp [4] [5]

22 Multidimensional Array in Java
int[][] twoDim; twoDim [ 0] = new int[2]; // row#0, 2 cols twoDim [ 1] = new int[4]; // row#1, 4 cols twoDim [ 2] = new int[6]; // row#2, 6 cols twoDim [ 3] = new int[8]; // row#3, 8 cols int elements [ ][ ] = new int[4][5]; // 4 rows, 5 cols each

23 Initializing Multidimensional Array
int[ ][ ] array_2d = { { 2, 4, 6, 8, 10 }, { 0, 1}, {20, 21, 22} }; long elements[ ][ ]; elements = new long[2][3]; for (int i=0; i<elements.length; i++) for (int j=0; j<elements[i].length; j++) elements[i][j] = 0L;

24 Java String

25 Java String สายอักษร (String) ในภาษาจาวา
ไม่ใช่เป็นเพียงเนื้อที่ในหน่วยความจำที่จองไว้สำหรับประเภทข้อมูล แต่เป็นวัตถุ (Object) ตัวแปร String ในภาษาจาวา เก็บค่าอ้างอิงของ String Object เมื่อมีการอ้างอิงค่าไปยัง String Object แล้ว จะไม่สามารถเปลี่ยนแปลงค่าใน String Object ได้โดยตรง แต่สามารถให้ค่าอ้างอิงใหม่ได้ ตัวอย่างเช่น String message = new String("Hello"); หรือ String message = "Hello";

26 String Declaration (1) การประกาศตัวแปร String ในภาษาจาวา
String empty = new String(""); String message = "Hello"; String repeat = message; "" xxx yyy zzz aaa bbb String empty stored at xxx stored at aaa After Line 1 Conceptual View Memory Address View Programming is about solving problems. Compare this to a program for a concert, for example. The program contains a list of what is to happen, in the order in which it will occur. Give an example of a small recipe and point out the data and the processing (ingredients and method). Point out the data and processing in instructions to change a car tyre. A knitting pattern specifies how many balls of wool of what thickness you need, and what size knitting needles. I wonder how many students know how to knit these days :-). There may not be many students who have filled in a tax form yet, either.

27 String Declaration (2) การประกาศตัวแปร String ในภาษาจาวา
String empty = new String(""); String message = "Hello"; String repeat = message; " Hello" message stored at yyy bbb String ”Hello" stored at bbb "" xxx yyy zzz aaa empty stored at xxx stored at aaa After Line 2 Conceptual View Memory Address View "" xxx yyy zzz aaa bbb String empty stored at xxx stored at aaa After Line 1 Conceptual View Memory Address View Programming is about solving problems. Compare this to a program for a concert, for example. The program contains a list of what is to happen, in the order in which it will occur. Give an example of a small recipe and point out the data and the processing (ingredients and method). Point out the data and processing in instructions to change a car tyre. A knitting pattern specifies how many balls of wool of what thickness you need, and what size knitting needles. I wonder how many students know how to knit these days :-). There may not be many students who have filled in a tax form yet, either.

28 String Declaration (3) การประกาศตัวแปร String ในภาษาจาวา
String empty = new String(""); String message = "Hello"; String repeat = message; Repeat stored at zzz " Hello" message stored at yyy bbb String ”Hello" stored at bbb "" xxx yyy zzz aaa empty stored at xxx stored at aaa After Line 3 Conceptual View Memory Address View " Hello" message stored at yyy bbb String ”Hello" stored at bbb "" xxx yyy zzz aaa empty stored at xxx stored at aaa After Line 2 Conceptual View Memory Address View Programming is about solving problems. Compare this to a program for a concert, for example. The program contains a list of what is to happen, in the order in which it will occur. Give an example of a small recipe and point out the data and the processing (ingredients and method). Point out the data and processing in instructions to change a car tyre. A knitting pattern specifies how many balls of wool of what thickness you need, and what size knitting needles. I wonder how many students know how to knit these days :-). There may not be many students who have filled in a tax form yet, either.

29 The “String” Class String Class
ค่าใน String Object ไม่สามารถเปลี่ยนแปลงได้ แต่สามารถให้ค่าอ้างอิงจากตัวแปร String ใหม่ ได้ Methods ของ String Class Create New String concat, replace, subString, toLowerCase, toUpperCase Search endswith, startswith, indexOf, lastIndexOf Comparison equals, equalsIgnoreCase, CompareTo others charAt, length

30 String Comparison การเปรียบเทียบ String ในภาษาจาวา
เปรียบเทียบค่าใน String Object ว่าเท่ากันหรือไม่ ใช้เมทธอด boolean equals(String s) เปรียบเทียบว่าตัวแปร String อ้างอิงถึง String Object เดียวกันหรือไม่ ใช้โอเปอร์เรเตอร์ (Operator) “==” เปรียบเทียบค่าใน String Object เรียงตามลำดับตัวอักษร ใช้เมทธอด int compareTo(String s) return ค่า 0 เมื่อเป็นค่าเดียวกัน, ค่าจำนวนเต็มบวกเมื่อ String Object นี้มากกว่า s และ ค่าจำนวนเต็มลบ เมื่อ String Object นี้น้อยกว่า s

31 Comparing String Contents
ตัวอย่าง String greeting = new String("Hello" ); String message = "Hello"; if (greeting.equals(message)) { System.out.println("equal content!"); } else { System.out.println("content NOT equal!"); } "Hello" message stored at yyy bbb String stored at bbb xxx yyy zzz aaa greeting stored at xxx stored at aaa Conceptual View Memory Address View

32 Comparing String Reference
ตัวอย่าง String greeting = new String("Hello" ); String message = "Hello"; if (greeting == message) { System.out.println("equal reference!"); } else { System.out.println(”reference NOT equal!"); } "Hello" message stored at yyy bbb String stored at bbb xxx yyy zzz aaa greeting stored at xxx stored at aaa Conceptual View Memory Address View

33 Comparing String Lexicographical Order
int compareTo(String s) return ค่า 0 เมื่อเป็นค่าเดียวกัน return ค่าจำนวนเต็มบวกเมื่อ String Object นี้มากกว่า s return ค่าจำนวนเต็มลบ เมื่อ String Object นี้น้อยกว่า s String greeting = new String("Hello" ); String message = ”World"; System.out.println(greeting.compareTo(message));

34 String Concatenation การนำ String มาเชื่อมต่อกัน
ใช้โอเปอร์เรเตอร์ Concatenate (+) แต่ไม่สามารถเปรียบเทียบ String โดยใช้โอเปอร์เรเตอร์แบบ Relational (>, <, >=, <=) และโอเปอร์เรเตอร์แบบ Equality ( ==, !=) ได้ String greetings = new String("Hello”); int ThisYear = 2001; String name = " World " ; String message = greetings + name; หมายเหตุ ใช้โอเปอร์เตอร์ (+) กับ String ได้เมื่อมีพจน์ใดพจน์หนึ่งเป็น String

35 Summary รู้จัก Java Array รู้จัก Java String


ดาวน์โหลด ppt Java Array and String Jarungjit Parnjai

งานนำเสนอที่คล้ายกัน


Ads by Google