2013-02-21 71 views
0

我在向我的“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]); 
     } 
    } 
} 

任何帮助将非常感激。

+0

你有你的班级命名方式申请,它看起来像一个行一组表格。你的意思是让公共类表扩展ArrayList '? – 2013-02-21 22:14:03

+0

你不能忽略.convertToString()的东西吗?如果你传递了一个'ArrayList '(你刚才创建的实际上是空的),那么你并不需要转换它。此外,你似乎在Row()的构造函数中使用申请人作为全局,这是不寻常的。 – Gijs 2013-02-21 22:17:18

+0

@SamDufel你是对的。我想要一个表作为行的集合。不过现在我做了改变。当我编译方法toArray()在表类中获取错误? “Table中的toArray()无法在java.util.List中实现toArray()返回类型void与java.lang.Object不兼容[]” – Hoggie1790 2013-02-21 22:30:07

回答

2

申请人(行)不是Row/Table类中的方法。

如果您希望将行添加到Arrraylist的行类,然后使用Add方法

applicant.add(行)方法。

另外,您应该注意,表和行关系不需要您创建扩展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 table = new Table();

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

//所以,现在当你将调用下面的方法列表,它会打印所有//其ID

table.list(); 
+0

我想将行添加到Table类中的arrayList 。对不起,如果它看起来真的不清楚。感谢您的帮助。 – Hoggie1790 2013-02-21 22:27:17

+0

@ Hoggie1790行类应该包含什么?我假设有一个申请人? – 2013-02-21 22:33:49

+0

是的,类行应该只包含一个申请人。 – Hoggie1790 2013-02-21 22:42:37

相关问题