You need to sign in to do that
Don't have an account?
David_
Collection Types - List<Account> vs Account[]
Hi,
I have a short, general question regarding collection types for sObjects when it comes to inline SOQL queries.
As we all know there are three collection types: Lists, Sets and Maps. I recently stumbled upon a Trailhead Project (https://trailhead.salesforce.com/content/learn/projects/quickstart-apex/quickstart-apex-2) where neither of these types where used in a method to retrieve a list of records using an SOQL query:
I guess one could have also go for this option:
Best,
David
I have a short, general question regarding collection types for sObjects when it comes to inline SOQL queries.
As we all know there are three collection types: Lists, Sets and Maps. I recently stumbled upon a Trailhead Project (https://trailhead.salesforce.com/content/learn/projects/quickstart-apex/quickstart-apex-2) where neither of these types where used in a method to retrieve a list of records using an SOQL query:
Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];I'm talking about the Account[] part of the code. Can someone explain to me what makes the [] type different from the List type? How does it behave and what are the particular use cases? When to use Account[] and when to use List<Account>?
I guess one could have also go for this option:
List<Account> oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];Any help appreciated!
Best,
David
When using one-dimensional lists of primitives or sObjects, you can also use more traditional array notation to declare and reference list elements. For example, you can declare a one-dimensional list of primitives or sObjects by following the data or sObject type name with the [] characters.
To answer the question about fixed arrays (lists), it all depends on how you try to put elements in them:
Even though the size of the previous String array is defined as one element (the number between the brackets in new String1), lists are elastic and can grow as needed provided that you use the List add method to add new elements. For example, you can add two or more elements to the colors list. But if you’re using square brackets to add an element to a list, the list behaves like an array and isn’t elastic, that is, you won’t be allowed to add more elements than the declared array size.
Thanks !
All Answers
When using one-dimensional lists of primitives or sObjects, you can also use more traditional array notation to declare and reference list elements. For example, you can declare a one-dimensional list of primitives or sObjects by following the data or sObject type name with the [] characters.
To answer the question about fixed arrays (lists), it all depends on how you try to put elements in them:
Even though the size of the previous String array is defined as one element (the number between the brackets in new String1), lists are elastic and can grow as needed provided that you use the List add method to add new elements. For example, you can add two or more elements to the colors list. But if you’re using square brackets to add an element to a list, the list behaves like an array and isn’t elastic, that is, you won’t be allowed to add more elements than the declared array size.
Thanks !
eg: Account[] oldAccounts;
Lists can be multi-dimensional
eg: List<List<Account>> oldAccounts ;