Reputation: 119
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.
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 ?
Upvotes: 2
Views: 74
Reputation: 2565
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
Upvotes: 1
Reputation: 96
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
Upvotes: 1