Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-类型不匹配:无法从元素类型字符串[]转换为列表<;字符串>;_Java - Fatal编程技术网

Java-类型不匹配:无法从元素类型字符串[]转换为列表<;字符串>;

Java-类型不匹配:无法从元素类型字符串[]转换为列表<;字符串>;,java,Java,我不熟悉getter和setter(基本上只是Java),但是我必须在这个任务中使用它们,所以如果我对它们做了什么错误,请告诉我 更重要的问题是我在我的方法上犯了错误。我的作业中针对我正在使用的特定方法的逐字说明如下: processData()方法应从ArrayList中获取所有记录数据,并通过setter将数据添加到每个实例字段中 但我一直收到一个错误,上面写着: 类型不匹配:无法从元素类型字符串[]转换为列表 在单词content上显示“for(List rowData:content)”

我不熟悉getter和setter(基本上只是Java),但是我必须在这个任务中使用它们,所以如果我对它们做了什么错误,请告诉我

更重要的问题是我在我的方法上犯了错误。我的作业中针对我正在使用的特定方法的逐字说明如下:

processData()方法应从ArrayList中获取所有记录数据,并通过setter将数据添加到每个实例字段中

但我一直收到一个错误,上面写着:

类型不匹配:无法从元素类型字符串[]转换为列表

在单词content上显示“
for(List rowData:content)
”的行上

非常感谢你能给我的任何帮助

到目前为止,我的代码是:

public abstract class Client {

  String file = "bank-Detail.csv";
  ArrayList<String[]> bank = new ArrayList<>();
  static Client o[] = new Client[12];

  public Client(String file) {
    this.file = file;
  }

  private String ID;
  private String Age;
  private String Sex;
  private String Region;
  private String Income;
  private String Married;
  private String Children;
  private String Car;
  private String Save_Act;
  private String Current_Act;
  private String Mortgage;
  private String Pep;

  public List<String[]> readData() throws IOException {
    //initialize variable
    int count = 0;
    //name file
    String file = "bank-Detail.txt";

    //make array list
    List<String[]> content = new ArrayList<>();

    //trycatch for exceptions
    try {
      //file reader
      BufferedReader br = new BufferedReader(new FileReader(file));
      //string to add lines to
      String line = "";
      while ((line = br.readLine()) != null) {
        content.add(line.split(","));
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    processData(content);
    return content;
  }

  public String getID() {
    return ID;
  }

  public void setID(String ID) {
    this.ID = ID;
  }

  public String getAge() {
    return Age;
  }

  public void setAge(String age) {
    this.Age = age;
  }

  public String getSex() {
    return Sex;
  }

  public void setSex(String sex) {
    Sex = sex;
  }

  public String getRegion() {
    return Region;
  }

  public void setRegion(String region) {
    Region = region;
  }

  public String getIncome() {
    return Income;
  }

  public void setIncome(String income) {
    Income = income;
  }

  public String getMarried() {
    return Married;
  }

  public void setMarried(String married) {
    Married = married;
  }

  public String getChildren() {
    return Children;
  }

  public void setChildren(String children) {
    Children = children;
  }

  public String getCar() {
    return Car;
  }

  public void setCar(String car) {
    Car = car;
  }

  public String getSave_Act() {
    return Save_Act;
  }

  public void setSave_Act(String save_Act) {
    Save_Act = save_Act;
  }

  public String getCurrent_Act() {
    return Current_Act;
  }

  public void setCurrent_Act(String current_Act) {
    this.Current_Act = current_Act;
  }

  public String getMortgage() {
    return Mortgage;
  }

  public void setMortgage(String mortgage) {
    this.Mortgage = mortgage;
  }

  public String getPep() {
    return Pep;
  }

  public void setPep(String pep) {
    Pep = pep;
  }

  public String toString() {
    return "[ID = " + ", age=";

    /// ect....
  }

  public void processData(List<String[]> content) {

    int index = 0;
    for (List<String> rowData : content) {
      //initialize array of objects
      //o[index] = new Client();

      //use setters to populate your array of objects
      o[index].setID(rowData.get(0));
      o[index].setAge(rowData.get(1));
      o[index].setRegion(rowData.get(3));
      o[index].setSex(rowData.get(2));
      o[index].setIncome(rowData.get(4));
      o[index].setMarried(rowData.get(5));
      o[index].setChildren(rowData.get(6));
      o[index].setCar(rowData.get(7));
      o[index].setSave_Act(rowData.get(8));
      o[index].setCurrent_Act(rowData.get(9));
      o[index].setMortgage(rowData.get(10));
      o[index].setPep(rowData.get(11));

      System.out.println(rowData);

      index++;
    }
  }

  public void printData() {
  }
}
公共抽象类客户端{
String file=“bank Detail.csv”;
ArrayList bank=新的ArrayList();
静态客户端o[]=新客户端[12];
公共客户端(字符串文件){
this.file=文件;
}
私有字符串ID;
私弦时代;
私密性;
私有字符串区域;
私人收入;
私人串结婚;
私生子;
私家车;
私人字符串保存法;
私人字符串当前_法案;
私人抵押;
私人字符串政治公众人物;
public List readData()引发IOException{
//初始化变量
整数计数=0;
//名称文件
String file=“bank Detail.txt”;
//制作数组列表
列表内容=新建ArrayList();
//trycatch查找异常
试一试{
//文件读取器
BufferedReader br=新的BufferedReader(新文件读取器(文件));
//要向其中添加行的字符串
字符串行=”;
而((line=br.readLine())!=null){
content.add(行分割(“,”);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}
过程数据(内容);
返回内容;
}
公共字符串getID(){
返回ID;
}
公共无效集合ID(字符串ID){
this.ID=ID;
}
公共字符串getAge(){
回归年龄;
}
公共无效设置(字符串期限){
这个。年龄=年龄;
}
公共字符串getSex(){
回归性;
}
公共无效集合(字符串性别){
性别=性别;
}
公共字符串getRegion(){
返回区;
}
公共无效集合区域(字符串区域){
区域=区域;
}
公共字符串getIncome(){
收益;
}
公共收入(字符串收入){
收入=收入;
}
公共字符串getMarried(){
结婚归来;
}
公共婚姻(字符串婚姻){
已婚=已婚;
}
公共字符串getChildren(){
返回儿童;
}
公共void集合子项(字符串子项){
儿童=儿童;
}
公共字符串getCar(){
返回车;
}
公共车辆(串车){
汽车=汽车;
}
公共字符串getSave_Act(){
返回保存行为;
}
公共无效设置保存动作(字符串保存动作){
保存动作=保存动作;
}
公共字符串getCurrent_Act(){
返回当前动作;
}
公共无效设置当前行为(字符串当前行为){
this.Current\u Act=Current\u Act;
}
公共字符串getMortgage(){
归还抵押;
}
公共抵押(串抵押){
这个。抵押=抵押;
}
公共字符串getPep(){
返回政治公众人物;
}
公共void setPep(字符串pep){
政治公众人物=政治公众人物;
}
公共字符串toString(){
返回“[ID=”+”,age=”;
///等等。。。。
}
public void processData(列表内容){
int指数=0;
对于(列表行数据:内容){
//初始化对象数组
//o[index]=新客户端();
//使用setter填充对象数组
o[index].setID(rowData.get(0));
o[index].setAge(rowData.get(1));
o[index].setRegion(rowData.get(3));
o[index].setSex(rowData.get(2));
o[index].setIncome(rowData.get(4));
o[index].setx(rowData.get(5));
o[index].setChildren(rowData.get(6));
o[index].setCar(rowData.get(7));
o[index].setSave_Act(rowData.get(8));
o[index].setCurrent_Act(rowData.get(9));
o[index].setMortgage(rowData.get(10));
o[index].setPep(rowData.get(11));
System.out.println(rowData);
索引++;
}
}
public void printData(){
}
}

正如错误提示的那样,…
内容是一个
列表
,因此它包含
字符串[]
元素,而不是
列表
元素


如果您的最终目标是一个
客户机
对象列表,那么只需使用方法
list readData()

List<Client> clients = new ArrayList<Client>();

BufferedReader br = null;
try {
    //file reader
    br = new BufferedReader(new FileReader(file));
    //string to add lines to
    String line = "";
    Client c = null;
    while ((line = br.readLine()) != null) {
        c = new Client();
        String[] rowData = line.split(",");
        c.setID(rowData.get(0));
        ...

        clients.add(c);
    }
} catch (FileNotFoundException e) {
  e.printStackTrace();
} finally {
    try {
        if (br != null) br.close();
    } catch (Exception e) {}
}  

return clients;
List clients=new ArrayList();
BufferedReader br=null;
试一试{
//文件读取器
br=新的BufferedReader(新的文件读取器(文件));
//要向其中添加行的字符串
字符串行=”;
客户端c=null;
而((line=br.readLine())!=null){
c=新客户端();
String[]rowData=line.split(“,”);
c、 setID(rowData.get(0));
...
添加(c);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}最后{
试一试{
如果(br!=null)br.close();
}捕获(例外e){}
}  
返回客户;

问题出在
processData
方法中。
内容的类型是
列表
。因此,当您尝试循环此列表时,每个元素都是
字符串数组
,而不是
列表
。此外,由于列表中的每个元素都是字符串数组,因此您可以访问通过使用普通数组方括号而不是列表的
get
方法创建列表。请尝试以下修复方法:

public void processData(List<String[]> content) {

    int index=0;
    for (String[] rowData: content){
        //initialize array of objects   
        //o[index] = new Client();

        //use setters to populate your array of objects
        o[index].setID(rowData[0]);
        o[index].setAge(rowData[1]);
        o[index].setRegion(rowData[3]);
        o[index].setSex(rowData[2]);
        o[index].setIncome(rowData[4]);
        o[index].setMarried(rowData[5]);
        o[index].setChildren(rowData[6]);
        o[index].setCar(rowData[7]);
        o[index].setSave_Act(rowData[8]);
        o[index].setCurrent_Act(rowData[9]);
        o[index].setMortgage(rowData[10]);
        o[index].setPep(rowData[11]);

        System.out.println(rowData);

        index++;
        }
}
public void processData(列表内容){
int指数=0;
for(字符串[]行数据:内容){
//初始化对象数组
//o[index]=新客户端();
//使用setter填充对象数组
o[index].setID(rowData[0]);
o[index].setAge(rowData[1]);
o[index].setRegion(rowData[3]);
o[索引]。