Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
Zedroid

# How to Implement 3 dimension array in Apex

Hi All,

I am trying to implement the three dimension array in apex, could any one let me know where I am getting struck.

`/* Here goes my java snippet */`
```private static int[][][] MAX_LENGTH = {
{ {41,  25,  17,  10},  {34,  20,  14,  8},   {27,  16,  11,  7},  {17,  10,  7,   4} },
{ {77,  47,  32,  20},  {63,  38,  26,  16},  {48,  29,  20,  12}, {34,  20,  14,  8} },
{ {461, 279, 192, 118}, {365, 221, 152, 93},  {259, 157, 108, 66}, {202, 122, 84,  52} },
{ {552, 335, 230, 141}, {432, 262, 180, 111}, {312, 189, 130, 80}, {235, 143, 98,  60} }};```
`/* Apex Snippet */`
```private static List<List<List<Integer>>> MAX_LENGTH = {
List<Integer> { List<Integer>﻿{41,  25,  17,  10}, List<Integer>﻿ {34,  20,  14,  8},  List<Integer>﻿ {27,  16,  11,  7},  List<Integer>﻿{17,  10,  7,   4} },
List<Integer> { {77,  47,  32,  20},  List<Integer>﻿{63,  38,  26,  16},  List<Integer>﻿{48,  29,  20,  12}, List<Integer>﻿{34,  20,  14,  8} },
List<Integer> { List<Integer>﻿{461, 279, 192, 118}, List<Integer>﻿{365, 221, 152, 93},  List<Integer>﻿{259, 157, 108, 66}, List<Integer>﻿{202, 122, 84,  52} },
List<Integer>  { List<Integer>﻿{552, 335, 230, 141}, List<Integer>﻿{432, 262, 180, 111}, List<Integer>﻿{312, 189, 130, 80}, List<Integer>﻿{235, 143, 98,  60} }};```

It would be appreciative if any one could give there ideas﻿ or solution. Thanks in advance.

Regards ,

Zubair Haris﻿.

Jake Gmerek

I could not find an elegant way to do this, but here is a way that works.  My code does not exactly fit your example, but you can see how it works...

```List<Integer> Temp1 = new List<Integer>{1,2,3,4};
List<Integer> Temp2 = new List<Integer>{1,2,3,4};
List<Integer> Temp3 = new List<Integer>{1,2,3,4};
List<Integer> Temp4 = new List<Integer>{1,2,3,4};
List<List<Integer>> TEMP5 = new List<List<Integer>>{Temp1,Temp2};
List<List<Integer>> TEMP6 = new List<List<Integer>>{Temp3, Temp4};
List<List<List<Integer>>> MAX_LENGTH = new List<List<List<Integer>>>{TEMP5, TEMP6};```

I hope this helps.

Jake Gmerek

I could not find an elegant way to do this, but here is a way that works.  My code does not exactly fit your example, but you can see how it works...

```List<Integer> Temp1 = new List<Integer>{1,2,3,4};
List<Integer> Temp2 = new List<Integer>{1,2,3,4};
List<Integer> Temp3 = new List<Integer>{1,2,3,4};
List<Integer> Temp4 = new List<Integer>{1,2,3,4};
List<List<Integer>> TEMP5 = new List<List<Integer>>{Temp1,Temp2};
List<List<Integer>> TEMP6 = new List<List<Integer>>{Temp3, Temp4};
List<List<List<Integer>>> MAX_LENGTH = new List<List<List<Integer>>>{TEMP5, TEMP6};```

I hope this helps.

This was selected as the best answer
spraetz

The three dimensional List is probably the best you can do in Apex.

If I were you, I'd build a layer of abstraction around it and create a simple API into the 3-D List so you didn't have to use it so awkwardly.

Zedroid

Hi Ryan﻿,

Could you please illustrate a bit so that how can I implement  the 3d concept using list.

Regards,

Zubair.

Tejashree Suryawanshi 10
List<List<List<String>>> numbers = new List<List<List<String>>>{
new List<List<String>>{ new List<String> {'T1','T2','T3','T4'}, new List<String> {'T11','T12','T13','T14'} },
new List<List<String>>{ new List<String> {'T11','T12','T13','T14'}, new List<String> {'T21','T22','T23','T24'} },
new List<List<String>>{ new List<String> {'T21','T22','T23','T24'}, new List<String> {'T31','T32','T33','T34'} }
};

System.debug(numbers[0]);
System.debug(numbers[0][1]);
System.debug(numbers[0][1][2]);