【问题标题】:How to create dynamic array in java depending up on input?java - 如何根据输入在java中创建动态数组?
【发布时间】:2017-12-19 06:29:07
【问题描述】:

在开始时,我正在启动 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]));
        }
    }   
}

【问题讨论】:

  • 使用列表或向量。
  • 使用列表 student = new ArrayList();
  • 是否必须使用常规数组?查看 collections api,您可以在其中找到许多列表实现,例如 Arraylist、Vector 等。

标签: java arrays multidimensional-array


【解决方案1】:

在从用户读取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 &lt;= rec,因为你使用了错误的索引,你会得到ArrayIndexOutOfBoundsException。请改用i &lt; rec

【讨论】:

    【解决方案2】:

    知道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.
    

    【讨论】:

      【解决方案3】:

      摆脱数组。你不需要它们。

      另外,如果你想要rec的记录数,不要从0循环到&lt;= rec,因为那将是rec + 1记录,所以我将循环改为使用&lt; 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));
              }
          }   
      }
      

      【讨论】:

        【解决方案4】:

        另一种解决方案:

        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 循环中,您需要使用 '
        • 用一个for循环来做所有事情就足够了

        【讨论】:

        • 感谢您的替代方式。但我怀疑所有学生对象的引用都是学生(即学生类实例)。所以基本上只有学生的对象引用会存储在列表中?? .如果我错了,请纠正我。
        • 在 for 循环的每次迭代中,我们创建 Student.class Student student = new Student(); 的新实例,然后设置其字段并将此实例添加到列表中:list.add(student);。所以,是的,所有实例都来自 Student.class,而我们的 list 将只包含学生。我们不会错过任何用户输入,并将所有学生保留在列表中。
        猜你喜欢
        • 1970-01-01
        • 2011-02-12
        • 2016-12-18
        • 1970-01-01
        • 2015-03-25
        • 2020-02-01
        • 2013-05-02
        • 2015-02-11
        • 2019-11-17
        相关资源
        最近更新 更多