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

时间:2018-06-20 05:54:08

标签: java collections java.util.scanner

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

在下面的程序中,我已经初始化了姓名和年龄,可以帮助我扫描用户的姓名和年龄,它应该进行比较,然后使用集合进行排序。

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


    }

}

3 个答案:

答案 0 :(得分:1)

你在这里

public class Control {

        private static Scanner sc;     //Making sc as a field variable so that we no longer need to explicitly close the resource 

        public static void main(String[] args) {
            System.out.println("Please enter your data ");
            sc = new Scanner(System.in);
            List<Student> studs = new ArrayList<Student>();
            Student stu1 = new Student(sc.nextInt(),sc.nextLine());  //Dynamically asking user to input age and name

            System.out.println("Please enter the Age and Name of the first user : ");
            Student stu2 = new Student(sc.nextInt(),sc.nextLine());

            System.out.println("Please enter the Age and Name of the second user : ");
            Student stu3 = new Student(sc.nextInt(),sc.nextLine());

            System.out.println("Please enter the Age and Name of the third user : ");
            Student stu4 = new Student(sc.nextInt(),sc.nextLine());

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


        }

}
  

仍有优化的方法,但是如果使用scanner从用户那里获取动态输入是您的唯一目标,那么就可以解决问题。

这是为您量身定制的代码。

public class Control {

    private static Scanner sc;     //Making sc as a field variable so that we no longer need to explicitly close the resource 

    public static void main(String[] args) {
        System.out.println("Please enter your data \n\n\n");
        sc = new Scanner(System.in);
        List<Student> studs = new ArrayList<Student>();

        // Asking the user to enter number of records he/she wishes to enter
        System.out.println("How many records would you like to enter : ");

        for(int  i  = sc.nextInt(); i > 0 ; i--){

            System.out.println("Please enter the Name : ");  //Asking user for the name
            String name = sc.next();

            System.out.println("Please enter the age of "+name+" :");  //Asking user for the age of that specific name
            int age = sc.nextInt();

            studs.add(new Student(age,name));
        }

        Collections.sort(studs,new StudAge());
        for(Student stud : studs) {
            System.out.println(stud);
        }


    }

}
  

最后一个代码节省了内存,因为它节省了创建多个参考变量,然后将它们add List所占用的空间。相反,我们直接在add的{​​{1}}方法内创建传递对象及其参数。

答案 1 :(得分:0)

这是一个简单的程序,可以根据需要添加任意数量的学生,也可以在定义ArrayList时使用任何比较器,因为ArrayList具有按自然顺序自动排序,例如(1、3、5、8)或(' A','B','C')。

$(document).ready(function(){
$(".value-swap div").each(function(e) {
    if (e != 0)
        $(this).hide();
});

$("#next").on('click',function(){
    if ($(".value-swap div:visible").next().length != 0)
        $(".value-swap div:visible").next().show().prev().hide();
    else {
        $(".value-swap div:visible").hide();
        $(".value-swap div:first").show();
    }
    return false;
});

$("#prev").on('click',function(){
    if ($(".value-swap div:visible").prev().length != 0)
        $(".value-swap div:visible").prev().show().next().hide();
    else {
        $(".value-swap div:visible").hide();
        $(".value-swap div:last").show();
    }
    return false;
});
});

}

如果您确实想使用Comparator,则有一个代码:

1st您需要创建进行排序的类

public class main {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    List<Student> studs = new ArrayList<>();
    System.out.println("How many students You want to add: ");
    int numberOfStudents = sc.nextInt();
    sc.nextLine(); //to consume /n
    for (int i = 0; i < numberOfStudents; i++) {
        System.out.println("Enter age: ");
        int age = sc.nextInt();
        sc.nextLine(); //to consume/n
        System.out.println("Enter name: ");
        String name = sc.nextLine();
        studs.add(age, name);
    }
    //You don`t need to sort cuz it is ArrayList and elements inside are automatically sorted by natural order
    for(Student stud : studs) {
        System.out.println(stud);
    }
}

}

然后在您的主类中添加:

public class AgeComparator implements Comparator<Student> {

@Override
public int compare(Student s1, Student s2) {
    if(s1.getAge() > s2.getAge())
        return 1;
    else if(s1.getAge() == s2.getAge())
        return 0;
    else
        return -1;
}

答案 2 :(得分:0)

package sys;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Control {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Student> studs = new ArrayList<Student>();
        System.out.print("Insert a number: ");
        int size = sc.nextInt();
        for (int i = 0; i < size; i++) {
            System.out.println("Enter age and name :");
            Student stu1 = new Student(sc.nextInt(), sc.nextLine());
            studs.add(stu1);
        }
        Collections.sort(studs, new StudAge());

        for (Student stud : studs) {
            System.out.println(stud);
        }

    }

}