By GokiSoft.com| 15:41 10/03/2021|
Java Advanced

[Share Code] Tìm hiểu về Exception - List & Map & ArrayList & Vector - Lập trình Java nâng cao



/*
 * 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 aptech.java2.lesson02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        //List
        ArrayList<Integer> list1 = new ArrayList<>(10);
        ArrayList<String> list = new ArrayList<>(10);
        list.add("vi du 1");
        list.add("vi du 2");
        list.add(0, "vi du 10");
        list.add("vi du 3");
        list.add("vi du 4");
        list.add("vi du 5");
        
//        throw new Exception("213123");//try catch -> error -> bo qua except.
        //System.exit(0);
        
//        list.remove(0);
        
        for (String string : list) {
            System.out.println(string);
        }
        
//        System.out.println("list[7] = " + list.get(7));
        
        Vector<String> v = new Vector<>();
        v.add("sdsad");
        v.add("43432");
        
        //HashMap -> key => value (localStorage)
        Map<String, Object> maps = new HashMap<>();
        maps.put("name", "Tran Van A");
        maps.put("age", 12);
        
        System.out.println("name: " + maps.get("name"));
        
        //Stack & Queue
    }
}



/*
 * 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 aptech.java2.lesson02;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int x, y;
        System.out.println("Test ... step 1...");
        System.out.println("x = ");
        System.out.println("Test ... step 2...");
        x = scan.nextInt();
        
        System.out.println("y = ");
        y = scan.nextInt();
        
//        System.gc();//Yeu cau truong trinh xu ly thu ron bon nho.
//        System.exit(0); //kill app -> Trong 1 so TH -> ket thuc chuong trinh.
        try {
            System.out.println("Test ... step 3...");
            int r = x/y;
            //TH -> y = 0 -> crash -> chet ung dung.
            
            System.out.println("Test ... step 4...");
            System.out.println("r = " + r);
            
            int[] t = new int[3];
            // length: 3, index: 0 -> 2
            t[2] = 12;
            t[3] = 100;
            System.out.println("t[3] = " + t[3]);
        } catch(ArithmeticException e) {
            System.out.println("Test ... step 5...");
//            e.printStackTrace();
        } catch(ArrayIndexOutOfBoundsException e) {
//            e.printStackTrace();
            
            System.out.println("Test ... step 6...");
        } finally {
            System.out.println("Finish");
            
            System.out.println("Test ... step 7...");
        }
        
//        try {
//            int r = x/y;
//            //TH -> y = 0 -> crash -> chet ung dung.
//
//            System.out.println("r = " + r);
//            
//            int[] t = new int[3];
//            // length: 3, index: 0 -> 2
//            t[2] = 12;
//            t[3] = 100;
//            System.out.println("t[3] = " + t[3]);
//        } catch(Exception e) {
//            e.printStackTrace();
//        } finally {
//            System.out.println("Finish");
//        }
        
        System.out.println("..........");
        
        System.out.println("Test ... step 8...");
        
        long t = System.currentTimeMillis();
        System.out.println("t = " + t);
        
        String s1 = "TRANVANDIEPS";
        String s2 = "[a-zA-Z]{2,20}";//Regular Express
        Pattern p = Pattern.compile(s2);
        Matcher m = p.matcher(s1);
        if(m.find()) {
            System.out.println("Thoa man");
        } else {
            System.out.println("No");
        }
        
        String s = "TRAN VAN DIEP";
        String[] list = s.split(" ");
        System.out.println("list[0] = " + list[0]);
        System.out.println("list[1] = " + list[1]);
        System.out.println("list[2] = " + list[2]);
        
        
    }
}





Tags:

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

5

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