简体   繁体   中英

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

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!

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

I also thought of using while and remove method:

SObject[] sList = [object1, object2,...];
if(sList.size() >= 10){
  while(sList.size() > 10){
    sList.remove(sList.size() - 1);
  }
}
System.debug('values: '+sList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM