Minh họa cách sử dụng ArrayList
Đây là ví dụ minh họa cách sử dụng ArrayList trong Java. Trong ví dụ này, mô tả và hướng dẫn cách cài đặt ArrayList dành cho người mới bắt đầu học Java cơ bản.
package demoArrayList1;
import java.util.*;
class student {
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
void intput() {
Scanner objSc = new Scanner(System.in);
System.out.print("id:");
this.setId(objSc.nextInt());
objSc.nextLine();
System.out.print("Name:");
this.setName(objSc.nextLine());
}
void output() {
System.out.println("id " + " " + this.getId() + "Name " + " " + this.getName());
}
}
public class demo {
public static void main(String[] args) {
ArrayList<student> list = new ArrayList<student>();
student st[] = new student[3];
for (int i = 0; i < st.length; i++) {
st[i] = new student();
st[i].intput();
list.add(st[i]);
}
for (int i = 0; i < list.size(); i++) {
list.get(i).output();
}
}
}
Trương Đình Huy
- Giải Mã Suy Nghĩ: Bước Đột Phá Biến Tín Hiệu Não Thành Ngôn Ngữ Viết
- Cuộc Cách Mạng Toàn Diện: Trí Tuệ Nhân Tạo (AI) Trong Ngành Y Tế
- Một số khác biệt giữa SQL Server và MySQL
- Benefits of Artificial Intelligence in Construction
- Special Purpose Entity (SPE)- Các Tổ Chức Được Thành Lập Với Mục Đích Đặc Biệt

