我在向我的「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