0

「Row」クラスの文字列を「Table」クラスに追加するのに問題があります。したがって、rowというクラスを作成するたびに、送信された文字列がクラス「Table」の同じインスタンスに追加されます。これが私のRowクラスです:

public class Row extends ArrayList<Table>{
public ArrayList<String> applicant;
public String row;
/**
 * Constructor for objects of class Row
 */
public Row()
{
    applicant = new ArrayList<String>();
    convertToString();
    applicants(row) //<------ This is the arrayList in the Table class that i wish to           add the row to
}

private void convertToString()
{
    for(int i = 0; i<applicant.size(); i++)
        {
            row = applicant.toString();
        }
}
}

これが私の「行」クラスです。

public class Table {

    public ArrayList<String> applicants;

    public String appArray[];

    /**
     * Constructor for objects of class Table
     */
    public Table() {
        applicants = new ArrayList<String>();
    }

    public void addApplicant(String app) {
        applicants.add(app);
        toArray();
    }

    public void toArray() {
        int x = applicants.size();
        appArray = applicants.toArray(new String[x]);
    }

    public void list() // Lists the arrayList
    {
        for(int i = 0; i < applicants.size(); i++) {
            System.out.println(applicants.get(i));
        }
    }

    public void listArray() // Lists the Array[]
    {
        for(int i = 0; i < appArray.length; i++) {
            System.out.println(appArray[i]);
        }
    }
}

どんな助けでも本当にありがたいです。

4

1 に答える 1

2

応募者(行)は、行/テーブルクラスのメソッドではありません。

RowクラスのArrraylistに行を追加する場合は、addメソッドを使用します。

apply.add(row)メソッド。

また、TableとRowの関係では、Tableクラスを拡張するRowクラスを作成する必要がないことに注意してください。2つの別々のクラスである必要があります。したがって、TableクラスとRowクラスには1対多の関係があります。したがって、Rowクラスの複数のインスタンスを追加できるようにTableクラスを変更します。

あなたが何をしようとしているのかわかりませんが、RowクラスにはrowIDとapplicantNameの2つが含まれている必要があるとしましょう。そして、各申請者を表す多くの行を持つテーブルクラス。

したがって、Rowクラスは次のようになります。

public class Row extends ArrayList<Table>{
String applicantName;
int applicantID;


/**
 * Constructor for objects of class Row
 */
public Row(int appID, String appName)
{
applicantName = appName;
applicantID = appID;

}

public getApplicantName(){
return applicantName;
}

public getApplicantID(){
return applicantID; 
}


}

そして、テーブルクラスは次のようになります。

public class Table {

    public ArrayList<Row> applicants;

    public String appArray[];

    /**
     * Constructor for objects of class Table
     */
    public Table() {
    applicants = new ArrayList<String>();
    }

    public void addApplicant(Row app) {
    applicants.add(app);

    }


    public void list() // Lists the arrayList
    {
    for(int i = 0; i < applicants.size(); i++) {
        Row row = (Row) applicants.get(i);  
        System.out.println("Applicant ID: "+ row.getApplicantID() +
        "  Applicant Name: " + row.getApplicantName());
    }
    }

}

したがって、上記のクラスを以下の方法で使用します。

Row r1 = new Row(1, "Tom");
Row r2 = new Row(2, "Hoggie");
Row r3 = new Row(3, "Julie");

テーブルテーブル=新しいテーブル();

table.addApplicant(r1);
table.addApplicant(r2);
table.addApplicant(r3);

//したがって、以下のメソッドリストを呼び出すと、//すべての応募者のIDが出力されます。

table.list();
于 2013-02-21T22:14:39.403 に答える