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
- Using A.I. Appropriately to Study English Pronunciation and Phonetics
- Phân tích đầu tư theo phương pháp Bottom-up và Top-down
- Sanfoli phát triển chương trình đào tạo nữ lãnh đạo
- Phương pháp điều hành kinh doanh trên toàn cầu của IKEA và vai trò của văn hóa tổ chức
- Specific Applications of AI in Architecture