Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套数组列表_Java_Arraylist - Fatal编程技术网

Java 嵌套数组列表

Java 嵌套数组列表,java,arraylist,Java,Arraylist,我有一个3级嵌套arrayList,如下所示: ArrayList<String> rowContents = new ArrayList(); ArrayList<ArrayList<String>> rows; ArrayList<ArrayList<ArrayList<String>>> page; rowContents.add("some content"); rows.add(

我有一个3级嵌套arrayList,如下所示:

   ArrayList<String> rowContents = new ArrayList();
   ArrayList<ArrayList<String>> rows;
   ArrayList<ArrayList<ArrayList<String>>> page;
    rowContents.add("some content");
    rows.add(rowContents);
    page.add(rows);

像这样使用3级嵌套ArrayList可以吗?或者,有更好的方法吗?

我建议为页面和行创建类

从内在上讲,他们可以将孩子列在一个列表中:

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

    //...
}



public class Page {
    List<Row> rows;

    //...
}
公共类行{
列出行内容;
页面父级;
//...
}
公共类页面{
列出行;
//...
}

页面和行的一些类怎么样?它更容易维护

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 */
}
公共类页面{
私有列表行=新的ArrayList();
}
公共类行{
/*在里面做些疯狂的事情*/
}
/*所有页面*/
列表页=新建ArrayList();
for(行:pages.getRows()){
/*对行的内容做一些疯狂的事情*/
}

“有更好的方法”用于什么?你的实际问题是什么?当你有3个嵌套的
ArrayList
s时,这可能是一个糟糕的设计。一个非常糟糕的设计。。使用类instead@LutzHorn-处理(或修改)嵌套arrayList设计的更好方法。是的,您可以这样做,但这并不是一个好主意。;)+1用于创建类。。你不认为它是树一样的结构吗。。对树对象进行分类很好