1

I know that the same post exists here for creating list dynamically but i have gone through the posts and got some idea through that but i am still unable to get what i need.

List<ZenObject__c> Zenobjs=[Select ZenLms_Name_del__c,ZenLms_SubText__c,ZenImage__c from ZenObject__c order by createddate];
list<list<ZenObject__c>> lst=new list<list<ZenObject__c>>();
system.debug(Zenobjs.get(0));
//Map<Integer,ZenObject__c > m1 = new Map<Integer,ZenObject__c>();
if (lst == null)
{
    lst = new List <List<ZenObject__c>> ();
}
for(integer i=0;i<2;i++){
    if (lst.size() == 0)
    {
        lst.add(new List <ZenObject__c> ());
    }
     else if (lst[i] == null)
    {
        lst.add(i, new List <ZenObject__c> ());
    }

     lst[0].add(Zenobjs.get(i));
}  

Here actually i am fetching 3 records from Zenobjs and storing in another list lst[0] .Similarly i need to fetch another set of 3 records and store it in lst[1] which will become list of lists. But i am getting error here: Line: 14, Column: 1 System.ListException: List index out of bounds: 1

Any help would be appreciated.

3 Answers 3

1

This is similar to handling two dimensional arrays. Below is sample example to fill and iterate over such Lists.

Creation:

List<List<ZenObject__c> listOfList = new List<List<ZenObject__c>>();

for(Integer i=3; i<3; i++)
{
    List<ZenObject__c> innerList = new List<ZenObject__c>();
    for(Integer j=3; j<3; j++) // OR  for(Integer j=0; j< Zenobjs.size(); j++)
    {
        innerList.add(new ZenObject__c()); // or existing instance innerList.add(Zenobjs.get(j));
    }
    listOfList.add(innerList);
}

Iteration:

// Iterating top list
for(List<ZenObject__c> innerList : listOfList)
{
    // iterating inner list
    for(ZenObject__c record : innerList)
    {
        System.debug(record);
    }
}
0
0

If you want to get data dynamically you can explore javascript remoting - its a very powerfull tool. I created a page using SLDS class where for getting the data i used javaScript remoting and to build the data table i used javascript

Note: the above solution goes if you are creating a legacy VF page.

For lightning we can go for aura:iterate option

0

I don't understand why are checking so many things..

I have modified your code. I guess this is what you were looking for:

List<account> Zenobjs=[Select id,name from account order by createddate limit 3];
list<list<account>> lst=new list<list<account>>();
lst.add(Zenobjs);

Zenobjs=[Select id,name from account order by createddate limit 4];
lst.add(Zenobjs);

system.debug(lst.get(1));

Please note: I have used account instead of your custom object ZenObject__c.

Size of lst is 2. Because we have added 2 items.

Let me know whether it clarifies your doubt.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .