我在向我的“Table”类的“Row”类中添加字符串时遇到了一些问题。所以每次我创建一个名为row的类时,它都会将提交的字符串添加到类“Table”的同一个实例中。 这里是我行类:将字符串添加到另一个类的数组列表中
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]);
}
}
}
任何帮助将非常感激。
你有你的班级命名方式申请,它看起来像一个行一组表格。你的意思是让公共类表扩展ArrayList'? –
2013-02-21 22:14:03
你不能忽略.convertToString()的东西吗?如果你传递了一个'ArrayList'(你刚才创建的实际上是空的),那么你并不需要转换它。此外,你似乎在Row()的构造函数中使用申请人作为全局,这是不寻常的。 –
Gijs
2013-02-21 22:17:18
@SamDufel你是对的。我想要一个表作为行的集合。不过现在我做了改变。当我编译方法toArray()在表类中获取错误? “Table中的toArray()无法在java.util.List中实现toArray()返回类型void与java.lang.Object不兼容[]” – Hoggie1790 2013-02-21 22:30:07