ArrayList< ClassName>中添加的方法不适用于参数(字符串) [英] method add in ArrayList<ClassName> is not applicable for the arguments (String)

查看:74
本文介绍了ArrayList< ClassName>中添加的方法不适用于参数(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取一个.txt文件,该文件包含多行,每行中都包含一个专业职业的名字.我已经创建了一个扫描器,但是每当我想添加扫描器刚刚读取的内容并尝试将其添加到arrayList时,都会弹出

I'm trying to read a .txt file that contains several lines with the name of a professional carreer in each one of them. I've created a Scanner but whenever I want to add what the scanner have just read and try to add it to the arrayList, this error pops up

ArrayList类型的add(ClassName)方法不适用于参数(字符串)

The method add(ClassName) in the type ArrayList is not applicable for the arguments (String)

ArrayList<Claseqla> clista = new ArrayList<Claseqla>();

        Scanner s = new Scanner(new File("texto.txt"));

        while(s.hasNextLine())
        {
            **clista.add(s.nextLine());**
        }

这是另一个类内的代码; 粗体标记的行是出现错误的地方. clista仅具有2个属性,但我想将它们添加到列表中,只填充一个String元素,另一个填充为空(是否可能?)

This is the piece of code inside another class; The bold marked line is where the error pops up. clista only has 2 attributes but I'd like to add them to the list with just one String element filled and the other empty (Is that even possible?)

推荐答案

s.nextLine()返回String.但是您的ArrayList具有通用类型Claseqla.您需要使用从s.nextLine()调用中获取的字符串创建一个Claseqla对象,然后将该对象添加到您的ArrayList中.

s.nextLine() returns a String. But your ArrayList has generic type Claseqla. You need to create a Claseqla object using the string you grab from the s.nextLine() call, and then add that object to your ArrayList.

这篇关于ArrayList&lt; ClassName&gt;中添加的方法不适用于参数(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆