繁体   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