By GokiSoft.com| 21:01 13/03/2020|
Java Basic

Share Code- Source Code Lession 2 - C1907L

Phần 1: 15-20

Giải thích các vấn đề các bạn gặp phải
Hướng dẫn chữa bài tập

Phần 2: Kiến thức mới

	=> Mệnh đề điều (if, else, switch)
	=> Loop (for, while, do..while)
	=> Mảng
		=> Mảng 1 chiều, mảng 2 chiều
		=> ArrayList
			=> ???



/*
 * 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 lession;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        int choose = 3;
        
        switch(choose) {
            case 1:
            case 2:
            case 3:
                System.out.println("ABC");
                break;
            case 4:
            case 5:
                System.out.println("DEF");
                break;
            default:
                System.out.println("FF");
                break;
        }
        
        //toan tu ++, --
        int x = 2;
        int y = x++;//y = 2, x = 3
        int t = ++x;//x = 4, y = 2, t = 4
        int z = x++ + ++x + y++ - --t;
        //z = 4(x=5) + 6 (x=6) + 2(y=3) - 3(t=3) = 9
        System.out.format("\nx = %d, y = %d, t = %d, z = %d\n", x, y, t, z);
        
        int num = 1, sum = 0;
        
        do {
            num++;
            sum = sum + num;
        } while(num <= 10);
        System.out.println("sum = " + sum);
        
        int product;
        for (int i = 0; i <= 5; i++) {
            product = i * 10;
            System.out.format("\n%d * 10 = %d\n", i, product);
        }
        
//        for (int i = 0;i <= 5;) {
//            product = i * 10;
//            System.out.format("\n%d * 10 = %d\n", i, product);
//        }
        
//        int i = 1;
//        for (;i <= 5;) {
//            product = i * 10;
//            System.out.format("\n%d * 10 = %d\n", i, product);
//        }

//        int i = 1;
//        for (;;i++) {
//            product = i * 10;
//            System.out.format("\n%d * 10 = %d\n", i, product);
//        }

//        int i = 1;
//        for (;;) {
//            product = i * 10;
//            System.out.format("\n%d * 10 = %d\n", i, product);
//        }

        //Mang 1 chieu trong java chung ta khai bao nhu nao
        //khai 1 mang gom 5 so nguyen
        int[] t1 = new int[5];
        //do dai cua mang >> length = 5
        //index : 0 -> length - 1 (index phai la so nguyen)
        t1[0] = 12;
        t1[1] = 11;
        t1[2] = 2;
//        t1[5] = 100;//error => Array Index of bound
//        
//        System.out.println("t[5] = " + t1[5]);//error => Array Index of bound

        //khai bao 1 mang gom 3 phan tu => 5, 2, 10
        int[] t2 = {5, 2, 10};
        System.out.println("Length : " + t2.length);
        
        //khai bao 1 mang k gom 5 phan tu => nhap tu ban phim
        int[] k = new int[5];
        
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < k.length; i++) {
            System.out.format("\nNhap k[%d] = ", i);
//            k[i] = input.nextInt();
        }
        
        for (int i = 0; i < k.length; i++) {
            System.out.format("\nk[%d] = %d", i, k[i]);
        }
        
        //foreach
        for (int value : k) {
            System.out.println(value);
        }
        
        //mang 2 chieu trong Java
        //khai bao 1 mang 2 chieu gom 2 hang, 3 cot
        int[][] m = new int[2][3];
        //ROW = 2, COLUMN = 3
        //index theo row => i => 0 -> ROW - 1
        //index theo column => j => 0 -> COLUMN - 1
        m[0][0] = 5;
        m[0][1] = 6;
        m[0][2] = 16;
        m[1][0] = 5;
        m[1][1] = 15;
        m[1][2] = 55;
        
        int[][] mm = {
            {5, 6, 16},
            {5, 15, 55}
        };
        
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                mm[i][j] = i * 10 + j;
            }
        }
        
        //ArrayList
        //Tim hieu ky ve cach khai bao mang 1 chieu
        int[] kk = new int[6];
        //gia su => trong khi chuong trinh chay => chung ta lai muon
       //mang kk => luu 20 phan tu => lam nhu nao???
       //do khai bao theo cach nay => kk => do dai co dinh
       //Nen trong TH nay => bat buoc phai tao mang moi
       //Co giai phap nao => giup chung ta khai bao 1 mang co do dai
       //dong => luc 1 phan tu, 2 phan tu, 5 ptu, 100 ptu
       //Trong TH nay => ArrayList
        ArrayList<Integer> list = new ArrayList<>();
        //mang list => do dai = 0
        list.add(1000);//index = 0 => length = 1
        list.add(10);//index = 1 => length = 2
        System.out.println("Length : " + list.size());
        
        //duyen qua phan tu trong mang
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        
        System.out.println("index (0) = " + list.get(0));
        
        //xoa 1 phan tu theo index thi lam the nao
        //gia su => chung ta xoa index = 0
        list.remove(0);
        //sau khi xoa xong => tu dong don mang
        System.out.println("Sau khi xoa.");
        System.out.println("index (0) = " + list.get(0));
        
        //cach dung foreach vs ArrayList
        for (int value : list) {
            System.out.println(value);
        }
        
        //cach su dung theo Express Lambda
        list.forEach((value) -> {
            System.out.println(value);
        });
    }
}

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

5

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