2013-02-21 67 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

相關問題