function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Minhaj Arifin 5Minhaj Arifin 5 

Returning an Array (or list)

I wanted to reach out to get an explanation of these questions. Your comments will help me better understand Apex. Thanks!

Three questions on returning an array or list of strings
Best Answer chosen by Minhaj Arifin 5
GauravGargGauravGarg

Hi Manhaj,

Please see below iimage

User-added image

Hope this helps. 

Thanks,

Gaurav  

All Answers

Raj VakatiRaj Vakati
Hi Minhaj ,
 
If you think Array vs list both are doing almost same work. but the only difference is Array size you need to define before instance as shown below
String[] arrays = new String[5];
Once you define you cannot increase the size.
 
In case of list you no need to worry of size and you can add any number elements (heap size Limits are there ) as shown below
List<String> str = new List<String>() ;
 
You can use both interchangeably.
 
String[] arr = new String[5]; 
arr[0] = 'Hello' ; 
arr[1] = 'Hello' ; 
arr[2] = 'Hello' ; 
arr[3] = 'Hello' ; 
arr[4] = 'Hello' ; 

System.debug(''+arr);
    List<String> lst = new List<String>();
lst.add('hello');
lst.add('hello');
lst.add('hello');

List<String> lst1 = new String[5];


 
Thank ,
Raj
 
 
 
GauravGargGauravGarg

Hi Manhaj,

Please see below iimage

User-added image

Hope this helps. 

Thanks,

Gaurav  

This was selected as the best answer
Minhaj Arifin 5Minhaj Arifin 5
Thank you Raj for explaining the difference between Array and Lists and how I can use them! 
Thank you Gurav for providing these answers! Really appreciate it guys.