簡體   English   中英

將字符串添加到另一個類的Array List中

[英]Adding a string to an Array List in another class

我在將“Row”類中的String添加到“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]);
        }
    }
}

任何幫助將非常感激。

applicants(row)不是Row / Table類中的方法。

如果要將行添加到Row類中的Arrraylist,請使用add方法

applicant.add(row)方法。

另外,您應該注意Table和Row關系不需要您創建擴展Table類的Row類。 它應該是兩個單獨的類。 因此Table和Row類將具有一對多的關系。 因此,修改Table類,以便它能夠添加Row類的多個實例。

我不知道你想要做什么但是讓我們說你的Row類應該包含兩個東西:rowID和applicantName。 還有一個表類,它將有許多行代表每個申請人。

所以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 = new Table();

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

//所以現在當你打電話給下面的方法列表時,它將打印所有申請人//他們的ID

table.list();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM