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
Noam Damri 3Noam Damri 3 

Dealing with two dimension array

Hey,

Ive created a two-dimension array and initialized it like that:

        list<list<integer>> two_d_array = new list<list<integer>>();
        integer[] one_d_array = new list<integer>();
       
        for(integer i=0 ; i<d2_size ; i++){
            for(integer j=0 ; j<d1_size ; j++){
                one_d_array.add(0);
            }
            two_d_array.add(one_d_array);
        }
i wanna approach a direct cell like that:
   two_d_array[2][3] = 1289;
    or (do the same thing)
    two_d_array.get(2).set(3, 1289);

The problem is, by doing it it assign all column [3] as 1289.
The array will be like:
[0][3] = 1289
[1][3] = 1289
[2][3] = 1289
etc.

So how can i approach a direct cell. A specific one, not the all column.

Thx a lot!

Best Answer chosen by Noam Damri 3
AshwaniAshwani
Correction to the above example:

Integer value = two_d_array.get(0).get(3)+900;

Integer index = 3; // element index

List<Integer> reference = new List<Integer>();
reference.addAll(two_d_array.get(0)); // get list from the list at index 0

reference.set(index, value); // set value to that list

two_d_array.set(0, reference); // set complete list to the index 0 again
    
for(integer i=0 ; i<5 ; i++){
       for(integer j=0 ; j<4 ; j++){
                System.debug(' @@@@@ Array['+i+']['+j+'] '+two_d_array.get(i).get(j));
       }
}

output:

User-added image

All Answers

AshwaniAshwani
Did you tried this, I am not sure but it should work:

Integer value = two_d_array.get(0).get(3);
Integer index = 3;
two_d_array.get(0).set(index, value);


Noam Damri 3Noam Damri 3
Same result  :(
AshwaniAshwani
Try this then:

Integer value = two_d_array.get(0).get(3);

Integer index = 3; // element index

List<Integer> tempArrayList = two_d_array.get(0); // get list from the list at index 0

tempArrayList.set(index, value); // set value to that list

two_d_array.set(0, tempArrayList) // set complete list to the index 0 again


Noam Damri 3Noam Damri 3
Hey Reid. Thx for your help.

It will get the same results. The whole column 3 will be filled with 'value'.

as the index is 3, two_d_array[0][3] , two_d_array[1][3], two_d_array[2][3], two_d_array[3][3] etc., will be all 'value'.


AshwaniAshwani
Yes, I know it is an example only. You can set any index which in list.
AshwaniAshwani
Correction to the above example:

Integer value = two_d_array.get(0).get(3)+900;

Integer index = 3; // element index

List<Integer> reference = new List<Integer>();
reference.addAll(two_d_array.get(0)); // get list from the list at index 0

reference.set(index, value); // set value to that list

two_d_array.set(0, reference); // set complete list to the index 0 again
    
for(integer i=0 ; i<5 ; i++){
       for(integer j=0 ; j<4 ; j++){
                System.debug(' @@@@@ Array['+i+']['+j+'] '+two_d_array.get(i).get(j));
       }
}

output:

User-added image

This was selected as the best answer
Noam Damri 3Noam Damri 3
It is working!!!
So actually it is the same logic as in the initilazing i did, you cant approach a direct cell but with a list.

Thank you!


pbattissonpbattisson
Noam,

If you are doing matrix work you may want to look at this starting linear algrebra library (https://github.com/pbattisson/Apex-Linear-Algebra)which provides some methods that may make working with this easier for you.