我正在使用以下方法从属性文件中读取:
public void loadConfigFromFile(String path) {
        Properties prop = new Properties();
        InputStream input = null;
        try {
            input = new FileInputStream(path);
            prop.load(input);
            /*saved like this:*/ //emails: abc@test.com, bbc@aab.com, ..
            String wordsS = prop.getProperty("keywords");
            String emailS = prop.getProperty("emails");
            String feedS = prop.getProperty("feeds");
            emails = Arrays.asList(emailS.split(",")); //ERROR !!
            words = Arrays.asList( wordsS.split(","))); //ERROR !!
            feeds = Arrays.asList( feedS.split(",")); //ERROR !!
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }要写入的字段声明如下:
    private ArrayList<String> emails = new ArrayList<>(
            Arrays.asList("f00@b4r.com", "test@test.com")
    );
    private LinkedList<String> words = new LinkedList<>(
            Arrays.asList("vuln", "banana", "pizza", "bonanza")
    );
    private LinkedList<String> feeds = new LinkedList<>(
            Arrays.asList("http://www.kb.cert.org/vulfeed",
                    "https://ics-cert.us-cert.gov/advisories/advisories.xml")
    );..so编译器向我展示了以下信息,我不知道如何使用这些信息:
不兼容的类型。必需的
ArrayList<String>但“asList”被推断为List<T>:不存在变量T的实例,因此List<T>符合ArrayList<String>
怎样才能避开这一切?
发布于 2017-08-28 09:40:16
问题是分配给emails = Arrays.asList(emailS.split(","));的声明变量的类型。
根据编译错误,它被声明为ArrayList,但Arrays.asList()返回List。
您不能将一个List分配给一个ArrayList,而您可以做相反的事情。
此外,Arrays.asList()返回一个私有类的实例:java.util.Arrays.ArrayList,它与您声明的ArrayList变量:java.util.ArrayList不同。因此,即使是向下转换到ArrayList,也无法工作并导致ClassCastException。
将声明的变量从ArrayList<String> emails更改为List<String> emails,并对另外两个ArrayList变量执行相同的操作。
https://stackoverflow.com/questions/45915933
复制相似问题