zoukankan      html  css  js  c++  java
  • ArrayList Student

    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;
        }
    }
  • 相关阅读:
    线程中消费者生产者的实例代码(synchronized关键字)
    ThreadLocal管理Connection
    ThreadLocal的练习代码
    JDK中线程组ThreadGroup
    创建线程的三种方式
    ES练习代码
    window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法
    ES配置文件中文版
    springmvc工作原理
    IE不兼容document.getElementsByClassName
  • 原文地址:https://www.cnblogs.com/xiaolei121/p/5759100.html
Copyright © 2011-2022 走看看