2

I am learning Java at the moment and I reached an obstacle while working with multidimensional Arraylists. I have 3 main array(lists) which I want to have some sort of relationship.

  1. Arraylist1: mainCategory (call it mainCat)
  2. Arraylist2: subCategory (call it subCat)
  3. Arraylist3: moderator (call it mod)

I'm trying to create a 3Darraylist which looks as follows:

[
  [[mainCat1, subCat1, subCat2, subCat3], [mod1, mod2, mod3, mod4]],
  [[mainCat2, subCat1, subCat2], [mod1, mod2, mod3]],
  [[mainCat3, subCat1, subCat2, subCat3, subCat4], [mod1, mod2, mod3, mod4, mod5]],
]

I have tried using:

ArrayList<ArrayList<ArrayList<String>>> my3dArraylist = new ArrayList<ArrayList<ArrayList<String>>>();

but I just could not understand more the single-dimension arraylist.

I want to populate 'my3dArraylist' with data using '.add' in order to make it look like the example above, how can I do that ?

2
  • What is your question? You state your don't understand it, but what do you want to do? Commented Feb 23, 2016 at 10:37
  • Sorry perhaps I wasnt very clear. I want to populate 'my3dArraylist' with data using '.add' in order to make it look like the example above. Thanks. Commented Feb 23, 2016 at 10:40

2 Answers 2

1

To be entirely honest, i don't think using a three dimensional arrayList is a clean practice. Maybe you should look at some some other design for your functionality. If you can clarify what you are trying to achieve, we might be able to help some more.

It is certainly possible though! Here is an example of working code:

public static <E> List<List<List<E>>> create(int dim1, int dim2, int dim3)
{
    List<List<List<E>>> list1 = new ArrayList<List<List<E>>>(dim1);
    for (int i = 0; i < dim1; i++)
    {
        List<List<E>> list2 = new ArrayList<List<E>>(dim2);
        for (int j = 0; j < dim2; j++)
        {
            List<E> list3 = new ArrayList<E>(dim3);
            for (int k = 0; k < dim3; k++)
            {
                list3.add(null);
            }
            list2.add(list3);
        }
        list1.add(list2);
    }
    return list1;
}

Reference: http://www.coderanch.com/t/431507/java/java/Dimensional-ArrayList

Good luck

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

2 Comments

Nick, thanks for your response. To be honest I would like to know the best approach from experts in the field. I build a list of categories (and sub categories) that is both flexible in size and that I can assign moderator to each. I also want to retrieve the moderator for each (sub)category at any time, kind of in a 'pairing' way of doing it.
I think that even though it will be more work, you should start to think more object oriented. You can easily make a Moderator, Category and SubCategory object. Make a list of SubCategories in Category and make a list of catagories in moderator. After that you can also ask the moderator from the category class by adding it as an attribute!
1

If you want to manipulate it manually here you go

Initialize your variables

        ArrayList<String> mainCategory = new ArrayList<>();
        ArrayList<String> moderator = new ArrayList<>();
        ArrayList<String> subCategory = new ArrayList<>();

        ArrayList<ArrayList<ArrayList<String>>> array3d = new ArrayList<>();

        ArrayList<ArrayList<String>> array2dFirstRow= new ArrayList<>();
        ArrayList<ArrayList<String>> array2dSecondRow= new ArrayList<>();
        ArrayList<ArrayList<String>> array2dthirdRow= new ArrayList<>();

        ArrayList<String> firstRowFirstColumn = new ArrayList<>();
        ArrayList<String> firstRowSecondColumn = new ArrayList<>(); 
        ArrayList<String> secondRowFirstColumn = new ArrayList<>();
        ArrayList<String> secondRowSecondColumn = new ArrayList<>();
        ArrayList<String> thirdRowFirstColumn = new ArrayList<>();
        ArrayList<String> thirdRowSecondColumn = new ArrayList<>();

Construct first 1D inner array[mainCat1, subCat1, subCat2, subCat3]

    firstRowFirstColumn.add(mainCategory.get(0));
    firstRowFirstColumn.add(subCategory.get(0));
    firstRowFirstColumn.add(subCategory.get(1));
    firstRowFirstColumn.add(subCategory.get(2));

Construct second 1D inner array[mod1, mod2, mod3, mod4]

    firstRowSecondColumn.add(moderator.get(0));
    firstRowSecondColumn.add(moderator.get(1));
    firstRowSecondColumn.add(moderator.get(2));
    firstRowSecondColumn.add(moderator.get(3));

Construct first 2D array [[mainCat1, subCat1, subCat2, subCat3], [mod1, mod2, mod3, mod4]]

    array2dFirstRow.add(firstRowFirstColumn);
    array2dFirstRow.add(firstRowSecondColumn);

Construct third 1D array [mainCat2, subCat1, subCat2]

    secondRowFirstColumn.add(mainCategory.get(1));
    secondRowFirstColumn.add(subCategory.get(0));
    secondRowFirstColumn.add(subCategory.get(1));

Construct fourth 1D array [mod1, mod2, mod3]

    secondRowSecondColumn.add(moderator.get(0));
    secondRowSecondColumn.add(moderator.get(1));
    secondRowSecondColumn.add(moderator.get(2));

Construct second 2D array [mod1, mod2, mod3]

    array2dSecondRow.add(secondRowFirstColumn);
    array2dSecondRow.add(secondRowSecondColumn);

Construct 3D array

[ [[mainCat1, subCat1, subCat2, subCat3], [mod1, mod2, mod3, mod4]], [[mainCat2, subCat1, subCat2], [mod1, mod2, mod3]] ]

    array3d.add(array2dFirstRow);
    array3d.add(array2dSecondRow);

now you can continue constructing until you end up with the list that you wanted

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.