【问题标题】:Adding a string to an Array List in another class将字符串添加到另一个类中的数组列表
【发布时间】:2013-02-21 22:09:29
【问题描述】:

我在将“行”类中的字符串添加到“表”类时遇到了一些问题。因此,每次我创建一个名为 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]);
        }
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 类命名的方式看起来就像一行是表的集合。你的意思是public class Table extends ArrayList&lt;Row&gt; 吗?
  • 你不能省略 .convertToString() 的东西吗?如果你传入一个ArrayList&lt;String&gt;(你刚刚创建的实际上是空的),你可能真的不需要转换它。而且,您似乎在 Row() 的构造函数中将申请者用作全局变量,这很不寻常。
  • @SamDufel 你是对的。我希望一个表是行的集合。然而,我现在做出了改变。当我在表类中编译方法 toArray() 时出现错误? “Table 中的 toArray() 无法实现 java.util.List 中的 toArray() 返回类型 void 与 java.lang.Object[] 不兼容”

标签: java arrays string class arraylist


【解决方案1】:

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

如果您希望将行添加到 Row 类中的 Arraylist 中,请使用 add 方法

applicant.add(row) 方法。

您还应该注意,Table 和 Row 关系不需要您创建扩展 Table 类的 Row 类。它应该是两个独立的类。因此 Table 和 Row 类将具有一对多的关系。因此修改 Table 类,使其能够添加 Row 类的多个实例。

我不知道您要做什么,但可以说您的 Row 类应该包含两个内容,即 rowID 和申请人名称。还有一个表类,它有很多行代表每个申请人。

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

【讨论】:

  • 我想在 Table 类的 arrayList 中添加行。对不起,如果它看起来真的不清楚。不过感谢您的帮助。
  • @Hoggie1790 行类应该包含什么?我假设一个申请人?
  • 是的,类 Row 应该只包含一个申请人。
  • 我已经修改了答案是你想要达到的目标吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
相关资源
最近更新 更多