function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gustavogustavo 

Four Dimensional Array

Hi, I'm trying to create a four dimensional array(4 columns, N rows) without success.

I succeed to create a two dimensional array.

Following is the code used

 

List  <List<decimal>> My_array_2D_1=new List  <List<decimal>>();
List  <List<decimal>> My_array_2D_2=new List  <List<decimal>>();
List <List <List<List<decimal>>>> My_array=new List <List <List<List<decimal>>>>();
List <decimal> column_1=new List <decimal>();
List <decimal> column_2=new List <decimal>();
List <decimal> column_3=new List <decimal>();
List <decimal> column_4=new List <decimal>();
column_1.add(1);
column_1.add(2);
column_2.add(10);
column_2.add(20);
column_3.add(100);
column_3.add(200);
column_3.add(1000);
column_3.add(2000);
My_array_2D_1.add(column_1);
My_array_2D_1.add(column_2);
My_array_2D_2.add(column_3);
My_array_2D_2.add(column_4);
//My_array.add(My_array_2D_2);

Without the last line the code runs OK. When I add the last line I receive the following error message :

Compile error at line 20 column 1
Incompatible element type LIST:LIST:Decimal for collection of LIST:LIST:LIST:Decimal

 

I would appreciate if someone can help me to define how to populate the 4D Array (My_array)

 

Gustavo

bob_buzzardbob_buzzard

Your problem is that you are attempting to add a 2d array as an element of a 4d array, which leaves you one dimension short.  My_array can only have elements that are 3d arrays.

 

If you want to add your 2d array you'll need to create a 3d array, store the 2d array inside that and then add that to the 4d array.  Something like:

 

 

List< List< List<Decimal>>> My_array_3D=new List <List <List<decimal>>>();
My_array_3D.add(My_array_2D_2);
My_array.add(My_array_3D);

 

 

gustavogustavo

Hi,

Your solution is OK but is not completely clear for me. I add two 2D array to the 3D array and then add it to the 4D array.

You can see below (System.debug)that both arrays(3D and 4D) have all the data and seems to be similar.

 

List  <List<decimal>> My_array_2D_1=new List  <List<decimal>>();
List  <List<decimal>> My_array_2D_2=new List  <List<decimal>>();
List< List< List<Decimal>>> My_array_3D=new List <List <List<decimal>>>();
List <List <List<List<decimal>>>> My_array=new List <List <List<List<decimal>>>>();
List <decimal> column_1=new List <decimal>();
List <decimal> column_2=new List <decimal>();
List <decimal> column_3=new List <decimal>();
List <decimal> column_4=new List <decimal>();
column_1.add(1);
column_1.add(2);
column_2.add(10);
column_2.add(20);
column_3.add(100);
column_3.add(200);
column_4.add(1000);
column_4.add(2000);
My_array_2D_1.add(column_1);
My_array_2D_1.add(column_2);
My_array_2D_2.add(column_3);
My_array_2D_2.add(column_4);
//My_array.add(My_array_2D_2);
My_array_3D.add(My_array_2D_1);
My_array_3D.add(My_array_2D_2);
My_array.add(My_array_3D);
System.debug(My_array_3D);
System.debug(My_array);

 

22:11:7.419|METHOD_ENTRY|[25,1]|System.debug(LIST:LIST:LIST:Decimal)
22:11:7.419|USER_DEBUG|[25,1]|DEBUG|(((1, 2), (10, 20)), ((100, 200), (1000, 2000)))
22:11:7.419|METHOD_EXIT|[25,1]|debug(ANY)
22:11:7.419|METHOD_ENTRY|[26,1]|System.debug(LIST:LIST:LIST:LIST:Decimal)
22:11:7.419|USER_DEBUG|[26,1]|DEBUG|((((1, 2), (10, 20)), ((100, 200), (1000, 2000))))

 

 

Thanks for your fast answer

 

Gustavo