3

I have a 3 level nested arrayList as follows:

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

In the code, within different loops, the arrayLists will be populated as follows:

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

Is it okay to use 3 level nested arrayLists like this? Or, is there a better approach?

6
  • "is there a better approach" For what? What is your actual problem? Commented Dec 19, 2013 at 11:35
  • 6
    When you have 3 nested ArrayLists, it might be a sign of a bad design. Commented Dec 19, 2013 at 11:35
  • A very bad design.. use classes instead Commented Dec 19, 2013 at 11:38
  • @LutzHorn - better approach for handling (or revising) a nested arrayList design. Commented Dec 19, 2013 at 11:42
  • 2
    Yes you can do it, but that doesn't make it a good idea. ;) Commented Dec 19, 2013 at 11:43

2 Answers 2

11

I suggest to create classes for page and rows.

Internaly they can hold their children in a list:

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

    //...
}



public class Page {
    List<Row> rows;

    //...
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for creating classes.. Wont you think it is tree like structure.. good to craate tree object
1

How about some classes for page and rows? It's easier to maintain!

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 */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.