By GokiSoft.com| 15:14 07/09/2022|
Java Basic

[Source Code] Chữa bài tập & Mang index, ArrayList, Vector, HashMap trong Java - C2109I

#Test02.java


/*
 * 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 com.gokisoft.c2109i.lesson02;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Test02 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int x, y;
        float s;
        
        System.out.println("Nhap x = ");
        x = scan.nextInt();
        System.out.println("Nhap y = ");
        y = scan.nextInt();
        
        try {
            s = x / y;
            System.out.println("s = " + s);
            
            int[] t = new int[2];
            t[3] = 100;
            System.out.println("t[3] !!!");
        } catch(ArithmeticException e) {
            System.out.println("ArithmeticException Error....");
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException Error....");
        } finally {
            //Giai phong bo nho -> ko de duoi try
            System.out.println("Finally...");
        }
    }
}


#Test01.java


/*
 * 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 com.gokisoft.c2109i.lesson02;

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

/**
 *
 * @author Diep.Tran
 */
public class Test01 {
    public static void main(String[] args) {
        //HashMap -> quan ly du lieu: key & value
        //key: fullname -> value: TRAN VAN A
        //Kieu du lieu: key -> int, String
        //value: int, float, char, boolean, String, Object Class, Array...
        
        HashMap<String, String> map = new HashMap<>();
        
        //Luu du lieu
        map.put("fullname", "TRAN VAN A");
        System.out.println("25: " + map.get("fullname"));
        
        map.put("fullname", "TRAN VAN B");
        System.out.println("28: " + map.get("fullname"));
        
        map.put("fullname", "TRAN VAN C");
        System.out.println("31: " + map.get("fullname"));
        
        map.put("age", "32");
        
//        map.remove("fullname");

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String val = entry.getValue();
            System.out.println("key = " + key + ", value = " + val);
        }
    }
}


#Test.java


/*
 * 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 com.gokisoft.c2109i.lesson02;

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        //Khai mang so nguyen su dung ArrayList
        //Khi tim hieu Vector: Chi thay ArrayList -> Vector: DONE
        //B1. Khai bao mang -> Mang so nguyen
        ArrayList<Integer> t = new ArrayList<>();
        //length: t.size(), index: 0 -> length - 1
        
        //B2. Them phan vao trong mang
        t.add(100); //length: 1, index: 0
        t.add(12); //length: 2, index: 0 -> 1
        t.add(22); //length: 3, index: 0 -> 2
        
        //B3. Lay du lieu trong mang
        // Xac dinh vi tri can lay ra: index = 1
        System.out.println("t[1] = " + t.get(1));
        
        for (int i = 0; i < t.size(); i++) {
            System.out.format("\nt[%d] = %d", i, t.get(i));
        }
        
        for (int v : t) {
            System.out.println("v = " + v);
        }
        
        //B4. Sua du lieu trong mang
        //sua index = 1 (12) -> 20
        t.set(1, 20);
        
        for (int v : t) {
            System.out.println("v = " + v);
        }
        
        //B5. Xoa phan tu trong mang
        //100, 20, 22
        //Xoa vi tri index = 1 (20) di -> lam cach nao
        t.remove(1);
        
        for (int v : t) {
            System.out.println("v = " + v);
        }
        
        //B6. Chen 1 phan tu vao trong mang
        //100, 22 -> chen 1 phan vao vi tri giua 100 va 22 (index = 1)
        t.add(1, 25);
        
        for (int v : t) {
            System.out.println("v = " + v);
        }
    }
}


#Main.java


/*
 * 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 com.gokisoft.c2109i.lesson02;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {

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

        //Khai niem mang index:
        int t[] = new int[100];
        int[] t2 = new int[100];
        //length: 100, index: 0 -> length - 1 : 0 -> 99
        //Note: so phan tu trong mang fixed

        //B1. Khai bao
        int k[] = new int[5];
        int[] k1 = new int[5];
        int[] k2 = {6, 1, 2, 10, 100};

        //B2. Them phan tu vao trong mang
        //Chuyen du lieu 12 vao trong mang tai vi tri index = 2
        k[2] = 12;
        k[0] = 1;
        k[1] = 16;
        System.out.println("Nhap k[3] = ");
        k[3] = abc.nextInt();

        //B3. Lay du lieu trong mang
        System.out.println("k[0] = " + k[0]);
        for (int i = 0; i < k.length; i++) {
            System.out.format("\nk[%d] = %d", i, k[i]);
        }

        //B4. Sua du lieu trong mang
        k[0] = 11;
        System.out.println("k[0] = " + k[0]);

        //B5. Xoa phan tu trong mang
        k[0] = 0;
        System.out.println("k[0] = " + k[0]);
    }
}


#BT1179.java


/*
 * 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 com.gokisoft.c2109i.lesson02;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class BT1179 {
    public static void main(String[] args) {
//        method01();
        System.out.println("Danh Fibonaci: ");
        for (int i = 0; i <= 10; i++) {
            System.out.print(fn(i) + ", ");
        }
    }
    
    /**
     * fn(5) = fn(4) + fn(3)
     *       = fn(3) + fn(2) + fn(2) + fn(1)
     *       = fn(2) + fn(1) + fn(1) + fn(0) + fn(1) + fn(0) + 1
     *       = fn(1) + fn(0) + 1 + 1 + 1 + 1 + 1 + 1
     *       = 1 + 1 + 6 = 8
     */
    static int fn(int n) {
        if(n == 0 || n == 1) return 1;
        
        return fn(n-1) + fn(n-2);
    }
    
    static void method01() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap max = ");
        int max = scan.nextInt();
        
        int f0 = 1, f1 = 1, fn = f0 + f1;
        System.out.println("Day fibonaci: ");
        System.out.print(f0 + ", " + f1);
        while(fn <= max) {
            System.out.print(", " + fn);
            f0 = f1;
            f1 = fn;
            fn = f0 + f1;
        }
    }
}


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 đó