2013-12-19 97 views
2

我有一個3級嵌套的ArrayList如下:嵌套的ArrayList

ArrayList<String> rowContents = new ArrayList(); 
    ArrayList<ArrayList<String>> rows; 
    ArrayList<ArrayList<ArrayList<String>>> page; 

在代碼中,不同的循環中,在的ArrayList將填充如下:

rowContents.add("some content"); 
    rows.add(rowContents); 
    page.add(rows); 

是不是好使用3級嵌套arrayLists像這樣?或者,有沒有更好的方法?

+0

「有沒有更好的方法」對於什麼?你真正的問題是什麼? – 2013-12-19 11:35:37

+6

當你有3個嵌套'ArrayList'時,它可能是一個糟糕的設計的標誌。 – Maroun

+0

非常糟糕的設計..使用類代替 –

回答

10

我建議爲頁面和行創建類。

Internaly他們可以保持自己的孩子在一個列表:

public class Row { 
    List<String> rowContent; 
    Page parent; 

    //... 
} 



public class Page { 
    List<Row> rows; 

    //... 
} 
+1

+1創建類..不覺得它是像樹一樣的結構..很好的創建樹對象 –

+0

@VineetSingla確實!感謝您指出了這一點! –

+0

謝謝。會做。 – janenz00

1

怎麼樣類頁和行?維護起來更容易!

public class Page{ 

private List<Row> rows = new ArrayList<Row>(); 

} 

public class Row{ 

/* do some crazy stuff in it*/ 

} 


/* all pages*/ 
List<Page> pages = new ArrayList<Page>(); 

for(Row row: pages.getRows()){ 
    /* do some crazy stuff with the content of the row */ 
} 
相關問題