By GokiSoft.com| 14:37 11/03/2020|
Java Basic

Share Code- Chia sẻ code java overview - List + ArrayList trong 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 lession8;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //List & ArrayList
        //n so nguyen => list se co do dai co dinh : 100 phan tu
        int[] list = new int[100];
        //mo rong dc do dai cua mang >> 100 => 100
        //chay ok nhat => list chi chua 2-10 => khong gian luu tru
        // se bi thua rat nhieu
        //co cach nao => quan ly 1 mang vs so phan tu dong
        //1, 3, 5, 10 , 100 => mo rong => toi uu bo nho
        
        List<Integer> list2 = new ArrayList<>();
        System.out.println("Length : " + list2.size());
        
        list2.add(1);//index = 0
        list2.add(2);//index = 1
        System.out.println("Length : " + list2.size());
        
        list2.add(3);//index = 2
        list2.add(4);//index = 3
        list2.add(6);//index = 4
        list2.add(8);//index = 5
        list2.add(12);//index = 6
        list2.add(100);//index = 7
        System.out.println("Length : " + list2.size());
        //ArrayList => tao ra 1 mang dong.
        System.out.println("Gia tri tai index = 5 >> " + list2.get(5));
        
        for (int i = 0; i < list2.size(); i++) {
            System.out.println("index = " + i + " co gia tri la = " + list2.get(i));
        }
        
        list2.remove(5);
    }
}


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

5

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