Minh họa cách cài đặt ArrayList trong Java


package demoArrayList;

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 input() {

                             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].input();

                                           list.add(st[i]);

                             }

                             for(int i=0;i<list.size();i++) {

                                           list.get(i).output();

                             }

              }

}

Trương Đình Huy