2016-05-19 8 views
0

私のタイトルは不明ですが残念ですが、別の言い方ではわかりません。 私は2つのパラメータ(3つの文字列と1つのオブジェクト)を取るオブジェクト "Rapport"を持っています。このオブジェクト "SortingParameter"は、多数のブール引数 "boolean ... args"をとります。私の目標は、テキストファイルの行を読み込み、これらの "Rapport"オブジェクトで作成されたArrayListを作成することです。このため、ラインを介してIループは:オブジェクトを作成中にタブの残りの部分にループする

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])); 
    } 

私は、オブジェクトを動的に私の分割[]タブの残りの部分を追加したいと思います。誰もこれがきれいにやり遂げることができるか知っていますか?

私のコードの残りの部分: 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

[] - タブをRapport-instanceに追加します。その後

public class Rapport { 
    //some attributes... 
    private String[] eitherColumns; 
    //getters and setters and so on.... 
} 

、あなたはファイルから行を読んだとき、あなたのラポールに、タブの残りの部分を追加するセッターまたは別のコンストラクタを使用します。 はちょうどあなたのラポール・クラスの属性として文字列、配列を提供します - インスタンス。

ファイルからString-Arrayを読み込むだけの別のコンストラクタを使用することもできます。クラスは配列要素を使って何をすべきかを決めることができます。これは便利なので、ビジネスロジックとデータを分離します。

+0

イテレータのような意味ですか? 3つ以上の単語の場合、3つの単語がタブに入り、このタブは新しいSortingParameter()に渡されますか? 私はこれについて考えましたが、簡単な方法があるかどうかを知りたかったのです。split []。REST()を使って、文字列をタブとしてではなく、litteralとして与えました。 – pathat0r

+0

タブを連結しますか?これが便利なのであれば、それを行うことができますが、元のデータにアクセスすることは常に有効です。別の方法を使って別の表現を得ることができます。私はコンストラクタに完全なString [] - 配列を与え、次にnomList、nomCategorieとnomRessourceをコンストラクタにセットすると思います。次に、 "new String [0]"がsplit [3](など)に設定されているこれらの3つの値なしで新しい配列を作成し、これを両方の列に保存します。次に、どちらかのリテラルを文字列として返すgetLiteratl()メソッドを記述できます。 – Supahupe

関連する問題