猿问

如何使用扫描仪在集合中动态添加数据

帮助我从用户那里扫描名称和年龄,而不是在ArrayList中预定义它。


我在以下程序中初始化了姓名和年龄,可以帮助我从用户那里扫描姓名和年龄,它应该进行比较,然后再使用collection进行排序。


public class Control {

    public static void main(String[] args) {


        List<Student> studs = new ArrayList<Student>();

        Student stu1 = new Student(22, "Gokul");

        Student stu2 = new Student(12, "Dolby");

        Student stu3 = new Student(24, "Rahul");

        Student stu4 = new Student(56, "Raj");


        studs.add(stu1);

        studs.add(stu2);

        studs.add(stu3);

        studs.add(stu4);


        Collections.sort(studs,new StudAge());

        for(Student stud : studs) {

            System.out.println(stud);

        }



    }


}


蝴蝶不菲
浏览 119回答 3
3回答
随时随地看视频慕课网APP

相关分类

Java
我要回答