【问题标题】:How can I turn this array into an ArrayList that does the same thing?我怎样才能把这个数组变成一个做同样事情的 ArrayList?
【发布时间】:2019-07-12 10:01:04
【问题描述】:

我想把这个程序的数组变成一个ArrayList。到目前为止我知道数组会变成

ArrayList<StudentList> list = new ArrayList<StudentList>();

每个list[i]都会变成:

list.get(i)

但是我不确定以下行将是什么以满足 ArrayList 版本

list[i] = new StudentList();

所以这里是完整的代码:

public static void main(String[] args) {

    StudentList[] list = new StudentList[5];

    int i;

    for (i = 0; i < list.length; ++i) {

        list[i] = new StudentList();

        System.out.println("\nEnter information of Student _" + (i + 1) + "\n");
        list[i].DataUserPrompt();

    }
    for (i = 0; i < list.length; ++i) {

        list[i].DisplayStudentData();
    }

    File file12 = new File("s_records.txt");

    try {

        PrintWriter output = new PrintWriter(file12);

        output.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java arrays oop arraylist


    【解决方案1】:

    [...] 到目前为止我知道数组会变成

    ArrayList<StudentList> list = new ArrayList<StudentList>();
    

    是的,这是正确的,但通常你会看到 List 而不是 ArrayList

    List<StudentList> list = new ArrayList<StudentList>();
    

    原因在于Program to an Interface的概念。想象一下,您想从 ArrayList 切换到其他类型为 List 的数据类型。如果您的代码现在已损坏,您可以轻松做到这一点而无需担心。


    但是我不确定以下行将是什么以满足 ArrayList 版本

    list[i] = new StudentList();
    

    由于list 现在是一个对象,你必须通过方法与它进行交互。在 oracle 的 ListArrayList 文档中,您会找到其中的许多。

    但是对于这种情况,您将需要add(E e)。您也可以在文档中查找E,这意味着:

    E - 此列表中元素的类型

    换句话说:E 将成为您的StudentList


    public static void main(String[] args) {
    
        List<StudentList> list = new ArrayList<StudentList>();
    
        int i;
    
        for (i = 0; i < list.size(); ++i) {
    
            list.add(new StudentList());
    
            System.out.println("\nEnter information of Student _" + (i + 1) + "\n");
            list.get(i).DataUserPrompt();
    
        }
        for (i = 0; i < list.size(); ++i) {
    
            list.get(i).DisplayStudentData();
        }
    
        File file12 = new File("s_records.txt");
    
        try {
    
            PrintWriter output = new PrintWriter(file12);
    
            output.close();
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用add(int index, E element) 在指定位置插入元素。

      所以list[i] = new StudentList(); 将变为list.add(i, new StudentList());

      但在您的情况下,列表尚未填充,因此您应该使用 add(E element) 否则您将获得 IndexOutOfBoundsException 因为索引将大于列表的当前大小:

      list.add(new StudentList());
      

      【讨论】:

      • 不需要add(index, obj)。一个简单的add(obj) 就可以了。例如。你不能做add(10, obj),除非列表已经有至少10个对象。
      • @Andreas 当然。我刚刚将该语句 exactly 转换为它的对应列表。看到整个代码,你是对的,没必要。
      【解决方案3】:

      new StudentList[5] 是一个大小为 5 的数组,包含所有值 null,因此您使用 new StudentList() 创建 5 个对象并分配给 5 个数组索引。

      但是,new ArrayList() 创建了一个 列表(大小为 0)。请注意,new ArrayList(5) 也会创建一个空列表,它只是经过优化以存储 5 个元素。所以你需要创建并添加 5 个对象:

      List<StudentList> list = new ArrayList<>();
      for (int i = 0; i < 5; i++) {
          list.add(new StudentList());
      }
      

      上面等价于数组代码:

      StudentList[] list = new StudentList[5];
      for (int i = 0; i < 5; i++) {
          list[i] = new StudentList();
      }
      

      在这两种情况下,您最终都会得到大小为 5 的 list,其中包含 5 个对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        • 2020-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-26
        • 1970-01-01
        相关资源
        最近更新 更多