如何根据输入在java中创建动态数组?

时间:2017-12-19 06:29:08

标签: java arrays multidimensional-array

一开始,我正在启动20大小数组字段以获取输入(即std_idname,...等)。 我想为这些字段创建一个动态数组,而不是在开始时初始化长度。 应根据用户输入的输入为动态数组分配其长度。 请在下面的代码中提供帮助。

public class Input {
    private int[] std_id = new int[20];// initiating 20 size 
    private String[] name = new String[20];
    private int[] age = new int[20];
    private String[] email = new String[20];;

    Scanner in = new Scanner(System.in);

    private final List<Student> Students = new ArrayList<Student>();

    public Input()
    {
        initInput();
    }

    public void initInput()
    {
         int rec;
         System.out.println("How  many records do u want to enter:");
         rec = in.nextInt();



        for(int i=0 ; i <= rec; i++)
        {
            std_id[i] = in.nextInt();
            name[i] = in.next();
            age[i] = in.nextInt();
            email[i] = in.next();
        }

        for(int i=0; i <= rec ; i++)
        {
            Students.add(new Student(std_id[i],name[i], age[i], email[i]));
        }
    }   
}

4 个答案:

答案 0 :(得分:0)

在用户阅读rec的值后立即使用以下行:

std_id = new int[rec];
name = new String[rec];
age = new int[rec];
email = new String[rec];

以下是完全更正的代码:

public class Input {
    private int[] std_id;// initiating 20 size 
    private String[] name;
    private int[] age;
    private String[] email;

    Scanner in = new Scanner(System.in);

    private final List<Student> Students = new ArrayList<Student>();

    public Input()
    {
        initInput();
    }

    public void initInput()
    {
         int rec;
         System.out.println("How  many records do u want to enter:");
         rec = in.nextInt();

        std_id = new int[rec];
        name = new String[rec];
        age = new int[rec];
        email = new String[rec];

        for(int i=0 ; i < rec; i++)
        {
            std_id[i] = in.nextInt();
            name[i] = in.next();
            age[i] = in.nextInt();
            email[i] = in.next();
        }

        for(int i=0; i < rec ; i++)
        {
            Students.add(new Student(std_id[i],name[i], age[i], email[i]));
        }
    }   
}

注意:在使用循环时,请不要使用i <= rec,因为您使用了错误的索引,您将获得ArrayIndexOutOfBoundsException。请改用i < rec

答案 1 :(得分:0)

在知道rec:

的值后初始化数组
int rec;
System.out.println("How  many records do u want to enter:");
rec = in.nextInt();

std_id = new int[rec];
age = new int[rec];
etc.

从顶部的类变量中删除初始化:

public class Input {    
    private int[] std_id;
    private String[] name;
    etc.

答案 2 :(得分:0)

摆脱阵列。你不需要它们。

此外,如果您想要rec条记录,请不要从0循环到<= rec,因为那会是rec + 1条记录,所以我改变了循环使用< rec,这是常用的方法。

Java命名约定是字段名称以小写字母开头,因此我将Students重命名为students

public class Input {
    Scanner in = new Scanner(System.in);

    private final List<Student> students = new ArrayList<Student>();

    public Input()
    {
        initInput();
    }

    public void initInput()
    {
        System.out.println("How  many records do u want to enter:");
        int rec = in.nextInt();

        for (int i = 0; i < rec; i++)
        {
            int std_id = in.nextInt();
            String name = in.next();
            int age = in.nextInt();
            String email = in.next();
            students.add(new Student(std_id, name, age, email));
        }
    }   
}

答案 3 :(得分:0)

还有一个解决方案:

class Input {
    private Scanner in = new Scanner(System.in);
    private List<Student> list = new ArrayList<>();

    public Input() {
        initInput();
    }

    public void initInput() {
        int rec;
        System.out.println("How  many records do u want to enter:");
        rec = in.nextInt();

        for (int i = 0; i < rec; i++) {
            Student student = new Student();
            student.setId(in.nextInt());
            student.setName(in.next());
            student.setAge(in.nextInt());
            student.setEmail(in.next());
            list.add(student);
        }
        // this line to stop entering data
        in.close();
        // this one just to show the result
        list.forEach(s -> System.out.println("Student ID " + s.getId() + ", name: " + s.getName()));
    }
}

评论:

  • 不需要使用数组,而是选择List!
  • 在for循环中,您需要使用&#39;&lt;&#39;不是&#39;&lt; =&#39;防止额外的迭代
  • 足以使用一个for循环来完成所有事情
相关问题