简体   繁体   English

将字符串添加到另一个类的Array List中

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

I'm having some trouble with adding a String in my "Row" class to my "Table" class. 我在将“Row”类中的String添加到“Table”类时遇到了一些麻烦。 So every time i create a class called row it will add the submitted string to the same instance of the class "Table". 所以每次我创建一个名为row的类时,它都会将提交的字符串添加到类“Table”的同一个实例中。 Here is my Row class: 这是我的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();
        }
}
}

Here is my "Row" class: 这是我的“行”类:

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

Any help would be really appreciated. 任何帮助将非常感激。

applicants(row) is not a method in the Row/Table class. applicants(row)不是Row / Table类中的方法。

If you wish to add the row to the Arrraylist in the Row class then use the add method 如果要将行添加到Row类中的Arrraylist,请使用add方法

applicant.add(row) method. applicant.add(row)方法。

Also you should note that Table and Row relationship does not require you to create a Row class extending the Table class. 另外,您应该注意Table和Row关系不需要您创建扩展Table类的Row类。 It should be two separate classes. 它应该是两个单独的类。 So Table and Row class will have a one to Many relationship. 因此Table和Row类将具有一对多的关系。 So modify the Table class such that it is able to add several instances of Row class. 因此,修改Table类,以便它能够添加Row类的多个实例。

I dont know what you are trying to do but lets say you a Row class should contain two things a rowID and applicantName. 我不知道你想要做什么但是让我们说你的Row类应该包含两个东西:rowID和applicantName。 And a table class which will have many rows representing each Applicant. 还有一个表类,它将有许多行代表每个申请人。

so Row class will somewhat look like this: 所以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; 
}


}

And the table class will look like this: 表类看起来像这样:

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

}

so use the above classes in below manner: 所以以下面的方式使用上面的类:

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

Table table = new Table(); 表table = new Table();

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

//So now when you will call the below method list it will print all the applicants with //their ids //所以现在当你打电话给下面的方法列表时,它将打印所有申请人//他们的ID

table.list();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM