How do you create a Two-Dimensional ArrayList?

How to create/traverse a multi-demintional arraylist or list?

  • I would like to know how to create a structure to hold three lists in the following format: [[[x],[y]],[z]] with x,y,z being three lists of strings. note that i want to have the x and y lists on the 3rd level down and the z list on the 2nd level down. I have tried creating 3 separate arraylists for x,y and z then another arraylist that holds x and y. The problem arise when i tried to create the outer most arraylist to hold all three, because something like ArrayList<ArrayList<ArrayList<String>>… would work for x and y but doesnt work for z because z is only 2 levels down. I would appreciate some tips on how to do this. I understand that what im trying to do is more like a tree but i do not know how to implement a non-binary tree in java (x,y,z having more then just 2 branches).

  • Answer:

    Any time you start putting library types inside library types inside library types, reconsider the design of your program. Maybe add some classes in lieu of the cascading ArrayLists you have right now. You admit that you should be using a tree instead. Java supports Trees (TreeModel http://docs.oracle.com/javase/6/docs/api/javax/swing/tree/TreeModel.html) Because right now, the way you have it organized, I can't even understand what you're trying to do. It seems very convoluted, when what you want to do is actually quite simple.

jiejiewu at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.