+ Start a Discussion
ZedroidZedroid 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake 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.

All Answers

Jake GmerekJake 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
spraetzspraetz

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.

ZedroidZedroid

Hi Ryan,

 

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

 

Regards,

Zubair.