package JihekuangjiaEXE; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class ArrayListedTest{ public static void main(String[] args) { String name = null; int id; int age; boolean y = true; Student stu; Scanner input = new Scanner(System.in); ArrayList<Student> arraylist = new ArrayList<Student>(); do { System.out.println("请输入学生学号:"); id = input.nextInt(); if(id == 0){ y = false; System.out.println("结束录入."); break; } System.out.println("请输入学生姓名:"); name = input.next(); System.out.println("请输入学生年龄:"); age = input.nextInt(); stu = new Student(id,name,age); arraylist.add(stu); } while (y); showInfo(arraylist); //调用输出方法 //修改学员信息 System.out.println("请输入要修改的学员学号:"); id = input.nextInt(); if(id > arraylist.size()){ System.out.println("对不起,没有该学员!"); } else{ System.out.println("请输入要修改的学员姓名:"); name = input.next(); System.out.println("请输入要修改的学员年龄:"); age = input.nextInt(); stu = new Student(id,name,age); arraylist.set(id-1, stu); showInfo(arraylist); } //删除学员信息 System.out.println("请输入要删除的学员学号:"); id = input.nextInt(); if(id > arraylist.size()){ System.out.println("对不起,没有该学员!"); } else{ arraylist.remove(id-1); showInfo(arraylist); } input.close(); } public static void showInfo(ArrayList<Student> arraylist){ Iterator<Student> it = arraylist.iterator(); System.out.println("学号 姓名 年龄"); while(it.hasNext()){ Student a = it.next(); System.out.println(a.getId()+" "+a.getName()+" "+a.getAge()); } } }
package JihekuangjiaEXE; public class Student { private String name = null; private long id; private int age; public Student() { super(); } public Student(long id, String name, int age) { super(); this.setName(name); this.setId(id); this.setAge(age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }