我试图创建一个带有链表的程序。

包com.delta.memory;

import java.util.ArrayList;

/**
 * Lists
 */
public class Lists {

    ArrayList<String> DaysOfTheWeek = new ArrayList<String>();

    DaysOfTheWeek.add("Sunday");
    DaysOfTheWeek.add("Tuesday");
    DaysOfTheWeek.add("Wednesday");
    DaysOfTheWeek.add("Thursday");

    DaysOfTheWeek.add(1, "Monday");

}


但是它给出了以下编译错误:

Error:(11, 22) error: <identifier> expected
Error:(11, 23) error: illegal start of type


还有一个警告:

Cannot resolve symbol 'add'


请帮忙。

最佳答案

您的代码应在方法内部。

public class Lists {

    public static void main (String[] args)
    {
        ArrayList<String> DaysOfTheWeek = new ArrayList<String>();

        DaysOfTheWeek.add("Sunday");
        DaysOfTheWeek.add("Tuesday");
        DaysOfTheWeek.add("Wednesday");
        DaysOfTheWeek.add("Thursday");

        DaysOfTheWeek.add(1, "Monday");
    }
}

10-06 07:18