Một số vấn đề cơ bản về Java

Nguyên tắc hoạt động của Java. Khái niệm

Java platform:

 + Java SE: JRE

 + Java ME: MIDP

2. Công cụ môi trường phát triển (JDK, Eclipse).

3. Cú pháp Java (package, tên file, tên lớp, cách thừa kế lớp và thực thi giao diện)

4. Các loại Interfaces và lớp mảng Java

 

ppt88 trang | Chuyên mục: Java | Chia sẻ: dkS00TYs | Lượt xem: 2985 | Lượt tải: 3download
Tóm tắt nội dung Một số vấn đề cơ bản về Java, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
à mỗi node không có quá 2 node con. Cây tìm kiếm nhị phân là cây nhị phân mà: Giá trị các nút thuộc cây con bên trái nhỏ hơn giá trị của nút cha. Giá trị các nút thuộc cây con bên phải lớn hơn giá trị của nút cha. Duyệt cây nhị phân Inorder traversal Preorder traversal Postorder traversal * Binary Search Tree Ví dụ về Binary Search Tree Cây con trái Cây con phải * Cài đặt Binary Search Tree public class TreeNode { int data; TreeNode leftNode, rightNode; public TreeNode( int nodeData ) { data = nodeData; leftNode = rightNode = null; } public void insert( int value ) { if ( value data ) { if ( rightNode == null ) rightNode = new TreeNode(value); else rightNode.insert( value ); } } } * Cài đặt Binary Search Tree public class Tree { private TreeNode root; public Tree() { root = null; } public void insertNode( int insertValue ) { if ( root == null ) root = new TreeNode( insertValue ); else root.insert( insertValue ); } public void preorderTraversal() { preorder( root ); } * Cài đặt Binary Search Tree public void inorderTraversal() { inorder( root ); } public void postorderTraversal() { postorder( root ); } private void preorder( TreeNode node ) { if ( node == null ) return; System.out.print( node.data + " " ); preorder( node.leftNode ); preorder( node.rightNode ); } * Cài đặt Binary Search Tree private void inorder( TreeNode node ) { if ( node == null )	 return; inorder( node.leftNode ); System.out.print( node.data + " " ); inorder( node.rightNode ); } private void postorder( TreeNode node ) { if ( node == null ) return; postorder( node.leftNode ); postorder( node.rightNode ); System.out.print( node.data + " " ); } } * Sử dụng Binary Search Tree public class TreeTest { public static void main( String[] args ) { Tree tree = new Tree(); int value; for ( int i = 1; i > Collection > Set > List > Map > SortedMap > SortedSet * Giao tiếp Collection Cung cấp các thao tác chính trên collection như thêm/xoá/tìm phần tử... Ví dụ: boolean add(Object element); boolean remove(Object element); boolean contains(Object element); int size(); boolean isEmpty(); Nếu lớp cài đặt Collection không muốn hỗ trợ các thao tác làm thay đổi collection như add, remove, clear... nó có thể tung ra ngoại lệ UnsupportedOperationException. * Giao tiếp Set Set kế thừa từ Collection, hỗ trợ các thao tác xử lý trên collection kiểu tập hợp Set không có thêm phương thức riêng ngoài các phương thức kế thừa từ Collection. Giao Tiếp Set định nghĩa bộ Collection không có phần tử trùng lắp * Giao tiếp SortedSet SortedSet kế thừa từ Set, nó hỗ trợ thao tác trên tập hợp các phần tử có thể so sánh được. Các đối tượng đưa vào trong một SortedSet phải cài đặt giao tiếp Comparable hoặc lớp cài đặt SortedSet phải nhận một Comparator trên kiểu của đối tượng đó. Một số phương thức của SortedSet: Object first(); // lấy phần tử đầu tiên (nhỏ nhất) Object last(); // lấy phần tử cuối cùng (lớn nhất) SortedSet subSet(Object e1, Object e2); // lấy một tập các phần tử nằm trong khoảng từ e1 tới e2. * Giao tiếp List List kế thừa từ Collection, nó cung cấp thêm các phương thức để xử lý collection kiểu danh sách (Danh sách là một collection với các phần tử Object được xếp theo chỉ số). Một số phương thức của List Object get(int index); Object set(int index, Object o); void add(int index, Object o); Object remove(int index); int indexOf(Object o); int lastIndexOf(Object o); * Duyệt collection Các phần tử trong collection có thể được duyệt thông qua Iterator. Các lớp cài đặt Collection cung cấp phương thức trả về iterator trên các phần tử của chúng. ... Collection c; Iterator it = c.iterator(); * Duyệt collection Iterator cho phép duyệt tuần tự một collection. Các phương thức của Iterator: boolean hasNext(); Object next(); void remove(); Ví dụ: Iterator it = c.iterator(); while ( it.hasNext() ) { Point p = (Point) it.next(); System.out.println( p.toString() ); } * Giao tiếp Map Giao tiếp Map cung cấp các thao tác xử lý trên các bảng ánh xạ. định nghĩa collection có các cặp khóa - giá trị. Một số phương thức của Map Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); ... * Giao tiếp Map Map cung cấp 3 cách view dữ liệu: View các khoá: 	Set keySet(); // Trả về các khoá View các giá trị: 	Collection values(); // Trả về các giá trị View các cặp khoá-giá trị 	Set entrySet(); // Trả về các cặp khoá-giá trị Sau khi nhận được kết quả là một collection, ta có thể dùng iterator để duyệt các phần tử của nó. * Giao tiếp SortedMap Giao tiếp SortedMap kế thừa từ Map, nó cung cấp thao tác trên các bảng ánh xạ với khoá có thể so sánh được. Giống như SortedSet, các đối tượng khoá đưa vào trong SortedMap phải cài đặt giao tiếp Comparable hoặc lớp cài đặt SortedMap phải nhận một Comparator trên đối tượng khoá. * Implementations Các cài đặt trong Collections Framework chính là các lớp collection có sẵn trong Java. Chúng cài đặt các collection interface ở trên để thể hiện các cấu trúc dữ liệu cụ thể. Ví dụ: mảng động, danh sách liên kết, cây đỏ đen, bảng băm... * Implementations List LinkedList ArrayList Map LinkedHashMap SortedMap HashMap TreeMap Set HashSet LinkedHashSet SortedSet TreeSet * Mô tả các cài đặt ArrayList: Mảng động, nếu các phần tử thêm vào vượt quá kích cỡ mảng, mảng sẽ tự động tăng kích cỡ. LinkedList: Danh sách liên kết 2 chiều. Hỗ trợ thao tác trên đầu và cuối danh sách. HashSet: Bảng băm. LinkedHashSet: Bảng băm kết hợp với linked list nhằm đảm bảo thứ tự các phần tử. TreeSet: Cây đỏ đen (red-black tree). * Mô tả các cài đặt HashMap: Bảng băm (cài đặt của Map). LinkedHashMap: Bảng băm kết hợp với linked list nhằm đảm bảo thứ tự các phần tử (cài đặt của Map). TreeMap: Cây đỏ đen (cài đặt của Map). * Ví dụ 1: TreeSet // This program sorts a set of names import java.util.*; public class TreeSetTest1 { public static void main(String[] args) { SortedSet names = new TreeSet(); names.add(new String("Minh Tuan")); names.add(new String("Hai Nam")); names.add(new String("Anh Ngoc")); names.add(new String("Trung Kien")); names.add(new String("Quynh Chi")); names.add(new String("Thu Hang")); System.out.println(names); } } * Ví dụ 2: Student Set class Student implements Comparable { private String code; private double score; public Student(String code, double score) { this.code = code; this.score = score; } public double getScore() { return score; } public String toString() { return "(" + code + "," + score + ")"; } * Ví dụ 2: Student Set public boolean equals(Object other) { Student otherStu = (Student) other; return code.equals(otherStu.code); } public int compareTo(Object other) { Student otherStu = (Student) other; return code.compareTo(otherStu.code); } } * Ví dụ 2: Student Set // This programs sorts a set of students by name and then by score import java.util.*; public class TreeSetTest2 { public static void main(String[] args) { SortedSet stu = new TreeSet(); stu.add(new Student("A05726", 8.5)); stu.add(new Student("A06338", 7.0)); stu.add(new Student("A05379", 7.5)); stu.add(new Student("A06178", 9.5)); System.out.println(stu); SortedSet sortByScore = new TreeSet(new Comparator() // create an inner class * Ví dụ 2: Student Set { public int compare(Object a, Object b) { Student itemA = (Student) a; Student itemB = (Student) b; double scoreA = itemA.getScore(); double scoreB = itemB.getScore(); 	 if ( scoreA < scoreB ) 	 return -1; 	 else 	 return 1; } }); // end of inner class sortByScore.addAll(stu); System.out.println(sortByScore); } } * Ví dụ 3: HashMap // This program stores a phone directory by hashing import java.util.*; public class MyMapTest { public static void main(String[] args) { Map phoneDir = new HashMap(); phoneDir.put("5581814", new String("Dept. Informatics")); phoneDir.put("8584490", new String("Defense Staff")); phoneDir.put("8587346", new String("Administrative Staff")); phoneDir.put("7290028", new String("Student Club")); // print all entries System.out.println(phoneDir); // remove an entry phoneDir.remove("8584490"); * Ví dụ 3: HashMap // replace an entry phoneDir.put("7290028", new String("International Relation")); // look up a value System.out.println(phoneDir.get("5581814")); // iterate through all entries Set entries = phoneDir.entrySet(); Iterator iter = entries.iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println("key=" + key + ", value=" + value); } } } * Các lớp bao Collection chỉ làm việc trên các Object. Những kiểu dữ liệu cơ bản như: byte, short, int, long, double, float, char, boolean không thể đưa được trực tiếp vào Collection mà phải thông qua các lớp bao. Các lớp bao: Byte, Short, Int, Long, Double, Float, Char, Boolean. Ví dụ: Integer intObject = new Integer(9); int value = intObject.intValue(); * Algorithms Các thuật toán được cài đặt như những phương thức tĩnh của lớp Collections Một số phương thức của Collections: static Object max(Collection c) static Object min(Collection c) static int binarySearch(List list, Object key) static void sort(List list) static void shuffle(List list) các phương thức tạo synchronized collection các phương thức tạo read-only collection * Ví dụ: Trộn bài import java.util.*; public class MyShuffleTest { public static void main(String[] args) { List numbers = new ArrayList(52); for (int i = 1; i <= 52; i++) numbers.add(new Integer(i)); System.out.println("Before shuffling:" + numbers + "\n"); Collections.shuffle(numbers); System.out.println("After shuffling:" + numbers + "\n"); } } * Collections Framework Legacy Implementations Là các lớp cũ được cài đặt bổ sung thêm các collection interface. Vector: Có thể thay bằng ArrayList Hastable: Có thể thay bằng HashMap Abstract Implementations Là các lớp trừu tượng đã cài đặt các collection interface mà ta có thể kế thừa để tạo ra các collection mới. AbstractCollection, AbstractSet, AbstractList... * Collections Framework 

File đính kèm:

  • pptMột số vấn đề cơ bản về Java.ppt
Tài liệu liên quan