很抱歉我的标题不清楚,但我不知道怎么说不同。 我有一个对象" Rapport"它需要几个参数(3个字符串和一个对象)。这个对象" SortingParameter"需要一些布尔参数" boolean ... args"。我的目标是读取文本文件中的行,创建一个由这些" Rapport"对象。为此,我循环了以下几行:
for (String line : Files.readAllLines(Paths.get("myTxt.txt"),Charset.forName("ISO-8859-1"))) {
String[] split = line.split(";");
if(split.length>3){
rapports.add(new Rapport(split[0],split[1],split[2],new SortingParameter(PROBLEM)));
}else{
rapports.add(new Rapport(split[0],split[1],split[2]));
}
我想动态地在对象中添加其余的split []标签。有人知道如何干净利落吗?
我的其余代码: Rapport.java
package model;
import static model.Constant.RESSOURCES;
public class Rapport {
private String nomListe;
private String nomRessource;
private String categorie;
private SortingParameter param;
private boolean hasParameter;
public Rapport(String nomListe, String nomRessource, String categorie, SortingParameter param){
this.nomListe = nomListe;this.nomRessource = RESSOURCES + nomRessource;
this.categorie = categorie;this.param = param;
this.hasParameter = true;
}
public Rapport(String nomListe, String nomRessource, String categorie){
this.nomListe = nomListe; this.nomRessource = RESSOURCES + nomRessource;
this.categorie = categorie; this.param = null; this.hasParameter = false;
}
/** Getters **/
public String getNomListe(){return nomListe;}
public String getNomRessource(){return nomRessource;}
public String getCategorie(){return categorie;}
public boolean hasParameter(){return hasParameter;}
public SortingParameter getParam(){return param;}
}
我的SortingParameter.java:
package model;
import java.util.ArrayList;
public class SortingParameter {
private ArrayList<Boolean> paramList;
public SortingParameter(boolean... args){
paramList = new ArrayList<>();
for (boolean arg : args) {
paramList.add(arg);
}
}
public ArrayList<Boolean> getParamList(){return paramList;}
}
答案 0 :(得分:0)
正如您所说,您希望将split [] - tab的其余部分添加到Rapport-instance。 只需在您的Rapport-class中提供String-Array作为属性:
public class Rapport {
//some attributes...
private String[] eitherColumns;
//getters and setters and so on....
}
然后,当您从文件中读取一行时,使用setter或其他构造函数将其余选项卡添加到Rapport-Instance。
您还可以使用单独的构造函数,该构造函数仅从文件中读取String-Array,并让类决定如何处理数组元素,这样更方便,从而将业务逻辑与数据分开。