Salesforce 在二维数组中添加值(索引超出范围 0)

Salesforce Add value in 2d array (Index out of bound 0)

我正在尝试在数组中添加数组,即二维数组,但在我尝试时抛出此错误:

List index out of bound 0

我正在尝试这一行:

df_depts_obs[df_mk_index]  = departmentobs_columns;

private void initializeInconsistentRecommendations() {
    recmn_qty = new List<Integer>();
    String mk_unique_Data;
    Integer bookmark;
    bookmark=0;
    mk_unique_Data='';
    totalCount = 0;
    inconsistentRecommendations = new List<AA_Recommendations__c>();
    inconsistentAccessorys = new List<AA_Accessory__c>();
    df_depts = new List<AA_DepartmentObservation__c>();
    df_depts_obs = new List<List<AA_DepartmentObservation__c>>();
    df_depts_recmds =  new List<List<AA_DepartmentRecommendation__c>>();
    df_depts_prds =  new List<List<AA_Recommendations__c>>();
    String df_mk_unique_dept=''; 
    Integer df_mk_index=-1;      

    departmentRecommendations_data  = new List<AA_DepartmentRecommendation__c>();
    List<AA_DepartmentObservation__c>departmentObservations = [SELECT Id, Name, Observation_ID__c,Department_ID__r.Name,Observation_ID__r.Observation__c FROM AA_DepartmentObservation__c 
                                                               WHERE Department_ID__r.Assessment__c = :assessment.Id LIMIT 10];       

    if (departmentObservations.size() > 0) {                   
        for (integer index = 0; index < departmentObservations.size(); index++ ) {
            AA_DepartmentObservation__c departmentObservation = departmentObservations[index];   
                  //df_depts.add(departmentObservation);    // FOR DEPARTMNET HEADING
                                   if(!df_mk_unique_dept.contains((String)departmentObservations[index].Department_ID__r.Name)){                    
                        df_mk_unique_dept += '<li>'+departmentObservations[index].Department_ID__r.Name+'</li>';                       
                        df_depts.add(departmentObservation);    // FOR DEPARTMNET HEADING
                        df_mk_index = df_mk_index + 1;
                    } 

                      List<AA_DepartmentObservation__c> departmentobs_columns = new List<AA_DepartmentObservation__c>();
                      departmentobs_columns.add(departmentObservation); 
                      df_depts_obs[df_mk_index]  = departmentobs_columns;

在Apex中,您不能使用数组符号[ ]添加对象,只能为数组中已经存在的索引设置值。

您可以声明数组的大小以便对其进行初始化,也可以使用 add 方法动态增大数组。

此示例显示了两个选项:

List<List<String>> stringList = new List<List<String>>(1);
List<String> innerListOne = new List<String> {'Red','Blue'};
List<String> innerListTwo = new List<String> {'One','Two'};
stringList[0] = innerListOne;
stringList.add(innerListTwo);