How can I turn this array into an ArrayList that d

2020-05-06 14:41发布

问题:

I want to make turn this program's array into an ArrayList. So far I know that the array will turn into

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

and that each list[i] will turn into:

list.get(i)

however I am not sure what the following line will be in order to satisfy the ArrayList version

list[i] = new StudentList();

so here is the full code:

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

回答1:

new StudentList[5] is an array of size 5, with all values null, so you create 5 objects using new StudentList() and assign to the 5 array indexes.

However, new ArrayList() creates an empty list (size 0). Note that new ArrayList(5) also creates an empty list, it is just optimized to store 5 elements. So you need to create and add 5 objects:

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

The above is equivalent to the array code:

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

In both cases you end up with a list of size 5, with 5 objects.



回答2:

You can use the add(int index, E element) to insert an element at the specified position.

So list[i] = new StudentList(); will become list.add(i, new StudentList());.

But in your case the list is not already populated, so you should use add(E element) otherwise you'll get IndexOutOfBoundsException because the index will be bigger than the current size of the list:

list.add(new StudentList());


回答3:

[...] So far I know that the array will turn into

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

Yes, this is correct, but normally you will see List instead of ArrayList.

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

The reason for this, is the concept of Program to an Interface. Imagine you want to switch from an ArrayList to an other data-type of type List.. You easily do it without to worry, if your code is now broken.


however I am not sure what the following line will be in order to satisfy the ArrayList version

list[i] = new StudentList();

Since list is now an object, you must interact with it through methods. On oracle's documentation for List and ArrayList you will find many of them.

But for this scenario you will need add(E e). You can look up E in the documentation too and it means:

E - the type of elements in this list

With other words: E will be your 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();
    }
}