gpt4 book ai didi

Java JCreator List 引用 ArrayList 不起作用?

转载 作者:行者123 更新时间:2023-11-30 07:46:28 26 4
gpt4 key购买 nike

您好,我一直在尝试使用以下代码使用 List 引用来引用 ArrayList:

import java.util.List;

public class Purse
{
private List<String> purseList;
public static final String EMPTY_STRING = "";

// constructs an ArrayList of Strings
public Purse()
{
purseList = new ArrayList<String>();
}

这给了我错误

"Purse.java:14: error: cannot find symbol
purseList = new ArrayList<String>();
^
symbol: class ArrayList
location: class Purse
1 error"

但是当我使用这套代码时没有问题,我该怎么办才能使用List而不是ArrayList

import java.util.ArrayList;

public class Purse
{
private ArrayList<String> purseList;
public static final String EMPTY_STRING = "";

// constructs an ArrayList of Strings
public Purse()
{
purseList = new ArrayList<String>();
}

最佳答案

您可以同时导入 ListArrayList!

import java.util.ArrayList;
import java.util.List;

public class Purse
{
private List<String> purseList;
public static final String EMPTY_STRING = "";

// constructs an ArrayList of Strings
public Purse()
{
purseList = new ArrayList<String>();
}
}

关于Java JCreator List 引用 ArrayList 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33866836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com