By GokiSoft.com| 16:31 20/03/2020|
Java Advanced

Share Code- Lession 1 - Java Advanced - T1907A

Share code


Java Advanced - ADF2

=======================

Lession 1


Học các chủ đề sau

- Hướng dẫn các bạn debug

=> Join project that

=> chua tung trai nghiem

=> debug

=> giup fix bug => xac dinh dung vi tri loi

=> project >> 1000 files >> ko doan dc

=> dinh vi => vi tri gay loi

=> break phan vung => thu hep berakpoint => dung khu gay error => tim bug

=> Log

=> trace (in dong thong diep) => dua vao do ma doan error

=> Tu may mo them tools ho tro debugs

=> debugs => rat nhieu ky thuat

- Exceptions

=> OK

- Collections (List, Map, ...)

=> Khai bao ve mang => quan ly danh sach object

=> List

=> ArrayList

=> Hoc cach sung => yeu cau dau tien

=> Hieu ban chat

ArrayList => Integer

int[] list = new int[];

=> add them 1 phan tu moi vao

=> kiem tra mang list => co kha nang luu them ko

=> max size

=> mo rong khong gian luu tru len

list2 => tang dung luong 

Move toan bo data list => list2

list2 => list

=> them 1 phan tu moi vao list

=> tang size (getSize)

=> Uu diem => nhanh

=> Nhuoc diem => du thua data

=> Vector

=> Cach dung => nhu a ArrayList => boi vi cung implement List

So sanh Vector & ArrayList

Vector => khong gian chua 10 phan tu

Vector => nhanh hon so vs ArrayList

Ton tai nguyen hon ArrayList

=> He thong chay rat nhanh => bo nho => ArrayList thay vi dung Vector

=> ????????????????

=> LinkedList

=> Cach dung => nhu ArrayList

=> Van de

Uu diem

- Them phan tu rat nhanh

Nhuoc diem

- Du lieu phan tan

- Truy cap rat cham

=> It dung

=> Se dung trong 1 so TH cu the thoi

Map

=> Quan ly phan tu mang theo dang key => value

=> HashMap


Stack & Queue

=> Quan ly 1 mang cac phan.

=> Stack => Ngan xep

=> Hinh dung cach to chuc => nhu xep 1 chong sach

=> LIFO (Last In First Out) => vao sau ra truoc

=> push (them 1 phan tu vao mang)

=> pop (lay 1 phan tu ra khoi mang) => lay phan tu sau cung

=> Queue => Hang doi

=> Hinh dung viec xep hang mua ve film

=> FIFO (First In First Out)

=> push (them 1 phan tu vao mang)

=> pop lay 1 ptu khoi mang => lay phan tu dau tien

- Generic


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lession1;

/**
 *
 * @author Diep.Tran
 */
public class Calculator {
    public static int plus(int x, int y) {
        return x + y;
    }
    
    public static int devide(int x, int y) throws CustomException {
        if(y==0) {
            //thong bao chuong trinh error => logic => crash
            //thong bao error toi lap trinh vien
            throw new CustomException(x, y, "Device by zero");
        } else {
            return x/y;
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lession1;

/**
 *
 * @author Diep.Tran
 */
public class CustomException extends Exception{
    int x, y;

    public CustomException(int x, int y, String message) {
        super(message);
        this.x = x;
        this.y = y;
    }

    @Override
    public String getMessage() {
        return "x ("+x+") / y("+y+") => " + super.getMessage();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lession1;

import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Phan 1 >> Exception >> Ngoai le >> chet app
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap x = ");
        int x = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap y = ");
        int y = Integer.parseInt(scan.nextLine());
//        
//        try {
//            int result = x/y;
//            System.out.format("\nKet qua %d/%d=%d", x, y, result);
//        } catch(ArithmeticException e) {
//            //nhay vao day
//            System.err.println("Devide by zero");
//        } finally {
//            //du code success & error
//            //Thi deu goi vao day
//            //Giai phong tai nguyen
//            System.out.println("Finish devide");
//        }

        int result;
        try {
            result = Calculator.devide(x, y);
            System.out.format("\nKet qua %d/%d=%d", x, y, result);
        } catch (CustomException ex) {
            System.out.println("Message >> " + ex.getMessage());
        }
        //Se gay exception => y = 0 => Exception => throw exception
        
        int[] list = new int[2];
        //length = 2, index = 0 -> 1
        list[1] = 8;
        try {
            list[2] = 6;
        } catch(ArrayIndexOutOfBoundsException e) {
            System.err.println("Index out of bouds >> " + e.getMessage());
        } finally {
            System.out.println("Finish TEST");
        }
        //ArrayIndexOutOfBoundsException => vang ra
        //Tat ca cac exception => de ke thua class object >> Exception
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lession1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        //add phan tu trong mang
        list.add(23);//index = 0
        list.add(120);//index = 1
        //hieu danh index vs List => them phan tu vao mang bang add()
        //Xoa phan tu khoi mang
        list.remove(0);//danh lai index cho mang List
        //lay phan tu trong mang
        int index0 = list.get(0);
        System.out.println("Value = " + index0);
        
        //cach 1
        for (int i = 0; i < list.size(); i++) {
            System.out.println("OKOK >> " + list.get(i));
        }
        
        //cach 2
        for (Integer v : list) {
            System.out.println("V = " + v);
        }
        
        //cach 3
        list.forEach((v) -> {
            System.out.println("V = " + v);
        });
        
        ///giong het nhau
        List<Integer> vector = new Vector<>();
        //add phan tu trong mang
        list.add(23);//index = 0
        list.add(120);//index = 1
        //hieu danh index vs List => them phan tu vao mang bang add()
        //Xoa phan tu khoi mang
        list.remove(0);//danh lai index cho mang List
        //lay phan tu trong mang
        int index1 = vector.get(0);
        System.out.println("Value = " + index1);
        
        //cach 1
        for (int i = 0; i < list.size(); i++) {
            System.out.println("OKOK >> " + list.get(i));
        }
        
        //cach 2
        for (Integer v : list) {
            System.out.println("V = " + v);
        }
        
        //cach 3
        list.forEach((v) -> {
            System.out.println("V = " + v);
        });
        
        //LinkedList
        List<Integer> li = new LinkedList<>();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lession1;

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

/**
 *
 * @author Diep.Tran
 */
public class Test2 {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("MIN", 0);
        map.put("ABC", 123);
        map.put("ABC", 1);
        map.put("ABC", 5);
        //Key => Value
        //Key => tu khoa => duy nhat => luu tru 1 phan tu nao do.
        
        //MIN => 0, ABC => 123
        //lay du lieu ra => key
        //muon lay key = MIN => gia tri cua no la bao nhieu
        int value = map.get("MIN");
        System.out.println("value = " + value);
        int value2 = map.get("ABC");
        System.out.println("value2 = " + value2);
    }
}


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

5

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