How can I get subList of a List in Apex Salesforce?

user3534513 picture user3534513 · May 26, 2014 · Viewed 9.3k times · Source

I have got a list of SObjects having N number of items/sObjects SObject[] sList = [sobject1, sboject2, sboject3, ........ , sobjectN]

How can I get just 10 items from the begining of the list

Thanks in advance!

Answer

Moti Korets picture Moti Korets · May 26, 2014

After running this code newList contains only first 10 objects from sList.

SObject[] sList = [sobject1, sboject2, sboject3, ........ , sobjectN];
List<SObject> newList = new List<SObject>();
    for(Integer i = 0; i< 10;i++){
    newList.add(sList[i]);
}

For more info please reffer to List documentation