Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java khởi tạo (initialize) List


Trong bài viết này, chúng ta sẽ hiểu cách khởi tạo List. List là collection có thứ tự cho phép lưu trữ và truy cập phần tử theo trình tự. Nó chứa các phương thức dựa trên chỉ số để chèn, cập nhật, xóa và tìm kiếm. Nó cũng có thể chứa phần tử trùng lặp.

Minh họa dưới đây:

Giả sử đầu vào là:

Run the program

Đầu ra mong muốn:

Initializing an integer list 
The elements of the integer list are: [25, 60] 
Initializing a string list 
The elements of the string list are: [Java, Program]

Thuật toán

Bước 1 - BẮT ĐẦU 
Bước 2 - Khai báo integer_list và string_list 
Bước 3 - Định nghĩa giá trị 
Bước 4 - Dùng List integer_list = new ArrayList() khởi tạo integer list 
Bước 5 - Dùng List string_list = new ArrayList() khởi tạo string list 
Bước 6 - Dùng .add() thêm item vào list 
Bước 7 - Hiển thị kết quả 
Bước 8 - DỪNG

Ví dụ 1

Gom thao tác dưới 'main'.

import java.util.*; 

public class Demo { 
    public static void main(String args[]) { 
        System.out.println("Required packages have been imported"); 
        System.out.println("\nInitializing an integer list"); 
        List integer_list = new ArrayList(); 
        integer_list.add(25); 
        integer_list.add(60); 
        System.out.println("The elements of the integer list are: " + integer_list.toString()); 
        System.out.println("\nInitializing a string list"); 
        List string_list = new ArrayList(); 
        string_list.add("Java"); 
        string_list.add("Program"); 
        System.out.println("The elements of the string list are: " + string_list.toString()); 
    } 
}

Kết quả

Required packages have been imported 
Initializing an integer list 
The elements of the integer list are: [25, 60] 
Initializing a string list 
The elements of the string list are: [Java, Program]

Ví dụ 2

Đóng gói thao tác vào hàm thể hiện lập trình hướng đối tượng.

import java.util.*; 

public class Demo { 
    static void initialize_int_list(){ 
        List integer_list = new ArrayList(); 
        integer_list.add(25); 
        integer_list.add(60); 
        System.out.println("The elements of the integer list are: " + integer_list.toString()); 
    } 
    
    static void initialize_string_list(){ 
        List string_list = new ArrayList(); 
        string_list.add("Java"); 
        string_list.add("Program"); 
        System.out.println("The elements of the string list are: " + string_list.toString()); 
    } 
    
    public static void main(String args[]) { 
        System.out.println("Required packages have been imported"); 
        System.out.println("\nInitializing an integer list"); 
        initialize_int_list(); 
        System.out.println("\nInitializing a string list"); 
        initialize_string_list(); 
    } 
}

Kết quả

Required packages have been imported 
Initializing an integer list 
The elements of the integer list are: [25, 60] 
Initializing a string list 
The elements of the string list are: [Java, Program]