Giới thiệu cách cài đặt ArayList trong ngôn ngữ lập trình Java


Giới thiệu cách cài đặt ArayList trong ngôn ngữ lập trình Java

 

package arrayListDemo;

 

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());

              }

}

 

class list {

              ArrayList<student> list = new ArrayList<student>();

 

              void input() {

                             student st[] = new student[3];

                             for (int i = 0; i < 3; i++) {

                                           st[i] = new student();

                                           st[i].input();

                                           list.add(st[i]);

                             }

              }

 

              void output() {

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

                                           list.get(i).output();

                             }

              }

 

              void delete(int id) {

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

                                           if (list.get(i).getId() == id) {

                                                          list.remove(i);

                                           }

                             }

              }

 

              void update(int id) {

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

                                           if(list.get(i).getId()==id) {

                                                          list.get(i).setName("Khoa");

                                                          list.set(i,list.get(i));

                                           }

                             }

              }

              void sort() {

                            

              }

}

 

public class demo {

 

              public static void main(String[] args) {

                             list l = new list();

                             l.input();

                             l.output();

                             l.update(1);

                             l.output();

              }

}

Trương Đình Huy