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
Brijesh KumarBrijesh Kumar 

how to declared two-dimensional array in apex class


Hi
i want to declare this.......

private Double[][] matrix;


but i am getting error.i am not find a solution to declaration of 2-D array.
Please give me  a solution to declared 2-D array because i am working of 2D matrix inversion.
It is most requirement in our project.


Thanks & Regards
Brijesh kumar baser
David VPDavid VP
Check the Apex Language Reference for Lists of Lists (I copied the example below straight from there) :

Code:
To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within <> characters. For example:

// Create an empty list of String
List<String> my_list = new List<String>();
// Create a nested list
List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();


 A list can contain any object, including Doubles


David



Message Edited by David VP on 10-06-2008 01:14 PM
Brijesh KumarBrijesh Kumar
Hi David VP
But i want to declare 2-D array because i want to calculate inverse of matrix.
I think for matrix declaration we use 2D array.
Matrix dimensions are 12*1.

So please give me a solution to declare two dimensional array.


Thanks & Regards
Brijesh Kumar Baser
David VPDavid VP

If you say that the matrix dimensions are 12*1 then I'm not sure if I'm really following you on the '2D' part of your question ?

Anyway, to see the List of List trick in action, throw this in your System Log window and execute it :

Code:
List<List<Double>> My_Array = new List<List<Double>>();

//I'll fill them up with a loop here  
for(Integer i = 0; i < 10; i++) {
   //create the 'columns'
   List<Double> columns = new List<Double>();
      for(Integer j = 0; j <10; j++) {
       Double d = j;
       columns.add(d);
      }
   My_Array.add(columns);
}

System.debug(My_Array);

//get any element in the matrix
System.debug('Element at row 2 column 4 : ' + My_Array.get(1).get(3));

//or if you want to change this value
My_Array.get(1).set(3, Double.valueOf('3.14159'));

//check if it did change
System.debug('Element at row 2 column 4 : ' + My_Array.get(1).get(3));

 
Hope this helps,


David

elinkelink

Here is an example on how initialize a two-dimensional array in apex.

 

   // Two demensonal array
  List<List<String>> arrayList = new List<List<String>>
  {
     new String[]{'Item 1', 'Item 2'},
     new String[]{'Item 1', 'Item 2'}
  };