By GokiSoft.com| 15:38 10/07/2023|
Java Advanced

[Share Code] Tìm hiểu về Exception & Collection trong Java - C2209I

Nội dung kiến thức:
	Exception
	Collection
		ArrayList
		Vector
		Hashmap
		Set
		Stack/Queue
	Generic
	Design pattern
	File
	Thread
		- Đa thread
		- Sync (sync, notify, wait)
	CSDL
		- CRUD (Thêm/sửa/xóa/hiển thị danh sách dữ liêu)
			- 1 bảng (student, room, product, ...)
			- nhiều bảng -> project
	Java Swing
		- Dự án CSDL <-> UI
	Note:
		Đặc biệt chú ý: Thiết kế database -> Chuẩn -> Phát triển được dự án
			table (column -> kiểu dữ liệu -> Độ dài của từng column)
			foreign key (chuẩn)
===========================================================================
Nội dung bài học:
	Exception
	Debug
		- runtime
		- log (write log -> do log -> Tim nguyen)
	StringBuilder & StringBuffer & String
	Collection
		ArrayList
		Vector
		Hashmap
		Set
		Stack/Queue

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson01;

import java.util.Scanner;

/**
 *
 * @author teacher
 */
public class Main {

    public static void main(String[] args) {
        //Logic
        Scanner scan = new Scanner(System.in);

        int x = 0, y = 0;
        float s;

        System.out.println("Nhap x = ");
        do {
            try {
                //Exception -> Lien quan toi viec nhap sai number.
                x = Integer.parseInt(scan.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Nhap lai: ");
            }
        } while (true);

        System.out.println("Nhap y = ");
        do {
            try {
                //Exception -> Lien quan toi viec nhap sai number.
                y = Integer.parseInt(scan.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Nhap lai: ");
            }
        } while (true);

        //Cach 1: Xu ly logic
        if (y != 0) {
            s = x / y;
            System.out.println("s = " + s);
        } else {
            System.out.println("Loi chia cho 0");
        }

        //Cach 2: try .. catch
        try {
            s = x / y;
            System.out.println("s = " + s);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }

        System.out.println("Finish app");
    }
}

#Test.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson01;

/**
 *
 * @author teacher
 */
public class Test {
    public static void main(String[] args) {
        //Noi chuỗi
        String s = "ABC";
        s += "XYZ";
        s += "sdfsdf";
        
        System.out.println("s = " + s);
        
        //TH: String dữ liệu lớn -> đọc file <-> Nối dữ liệu khi đọc file
        StringBuilder builder = new StringBuilder();
        builder.append("sdfsdfdshfkjhds");
        builder.append("sdfkjsdhfsdkjhf");
        builder.append("sdfxcv324234");
        
        System.out.println(builder.toString());
        
        StringBuffer buffer = new StringBuffer();
        buffer.append("sdfsdfdshfkjhds");
        buffer.append("sdfkjsdhfsdkjhf");
        buffer.append("sdfxcv324234");
        
        System.out.println(buffer.toString());
    }
}

#Test1.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson01;

import java.util.ArrayList;
import java.util.Vector;

/**
 *
 * @author teacher
 */
public class Test1 {
    public static void main(String[] args) {
        //Tim hieu List
        //Khai bao mang
        ArrayList<String> arr1 = new ArrayList<>();
        
        //Them phan tu
        arr1.add("Vi du 1");
        arr1.add("Vi du 2");
        arr1.add("Vi du 3");
        arr1.add("Vi du 4");
        arr1.add("Vi du 5");
        
        //lay du lieu ra
        System.out.println(arr1.get(0));
        
        //duyet cac phan tu trong mang
        for (String v : arr1) {
            System.out.println(v);
        }
        
        //chen du lieu
        arr1.add(1, "Vi du 1111");
        for (String v : arr1) {
            System.out.println(v);
        }
        
        //xoa cac phan tu trong mang
        arr1.remove(3);
        for (String v : arr1) {
            System.out.println(v);
        }
        
        arr1.clear();
        
        //Tim hieu List
        //Khai bao mang
        Vector<String> arr2 = new Vector<>();
        
        //Them phan tu
        arr2.add("Vi du 1");
        arr2.add("Vi du 2");
        arr2.add("Vi du 3");
        arr2.add("Vi du 4");
        arr2.add("Vi du 5");
        
        //lay du lieu ra
        System.out.println(arr2.get(0));
        
        //duyet cac phan tu trong mang
        for (String v : arr2) {
            System.out.println(v);
        }
        
        //chen du lieu
        arr2.add(1, "Vi du 1111");
        for (String v : arr2) {
            System.out.println(v);
        }
        
        //xoa cac phan tu trong mang
        arr2.remove(3);
        for (String v : arr2) {
            System.out.println(v);
        }
        
        arr2.clear();
    }
}

#Test02.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson01;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author teacher
 */
public class Test02 {
    static class Student {
        String name;
        String rollNo;

        public Student(String name, String rollNo) {
            this.name = name;
            this.rollNo = rollNo;
        }

        @Override
        public String toString() {
            return "Student{" + "name=" + name + ", rollNo=" + rollNo + '}';
        }
    }
    
    public static void main(String[] args) {
        Map<String, String> maps = new HashMap<>();
        //Map<Key, Value>
        //Key: int, String
        //Value: int, String, double, boolean, Object, ...
        maps.put("fullname", "TRAN VAN A");
        System.out.println(maps.get("fullname"));
        
        maps.put("fullname", "TRAN VAN B");
        System.out.println(maps.get("fullname"));
        
        maps.put("age", "20");
        
        System.out.println(maps.get("age"));
        
        Map<String, Student> stdList = new HashMap<>();
        stdList.put("R001", new Student("A", "R001"));
        stdList.put("R002", new Student("B", "R002"));
        stdList.put("R003", new Student("C", "R003"));
        
        Student std = stdList.get("R001");
        System.out.println(std);
        
        for (Map.Entry<String, Student> entry : stdList.entrySet()) {
            String rollNo = entry.getKey();
            Student std2 = entry.getValue();
            System.out.println(std2);
        }
    }
}

#Test03.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson01;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.PriorityQueue;
import java.util.Stack;

/**
 *
 * @author teacher
 */
public class Test03 {
    public static void main(String[] args) {
        //FIFO
        PriorityQueue<String> queue = new PriorityQueue<>();
        
        queue.add("vi du 1");
        queue.add("vi du 2");
        queue.add("vi du 3");
        
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        Deque<String> queue2 = new ArrayDeque<>();
        queue2.add("vi du 1");
        queue2.add("vi du 2");
        queue2.add("vi du 3");
        
        System.out.println(queue2.pop());
        System.out.println(queue2.pop());
        System.out.println(queue2.pop());
        
        //Stack: LIFO
        Stack<String> stack2 = new Stack<>();
        stack2.push("Vi du 1");
        stack2.push("Vi du 2");
        stack2.push("Vi du 3");
        
        System.out.println(stack2.pop());
        System.out.println(stack2.pop());
        System.out.println(stack2.pop());
    }
}
Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó