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
OnurKOnurK 

Array vs List

Hi All,

 

Can anyone please explain me the difference between List and Array. 

 

Test__c[] t = new Test__c[0];

vs

List<Test__c> t = new List<Test__c>();

 

Thanks 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26
List can be multidimensional

A "array notation" of a list using [] can only be one dimensional

All Answers

MikeGillMikeGill

With an array you always know where you're and a list you don't


ChizChiz

As I understand from documentation no difference. It's just a mater of habit.

OnurKOnurK

This is what I am getting, I guess you are right.

 

Thanks

kaszy86kaszy86

Ahmmm... i think those are very very short descriptions about what you can do with a list :o

 

First of all: yes, with an array you always know the position of that object, number or whatever you keep in there... but a big inconvenient is the way you move in the positions of an array, with a list you have a lot of methods to make the size dynamic (this is something you cant do with an array), also with a list you never get empty positions (you just add or remove items and thats it!), you can find an object without implementing a method yourself because the list already has it (it is named find, of course), and you can loop it with a for-each loop.

 

In the other hand, and talking about pros and cons: a list definitely needs more memory... but you have to consider also the kind of data you're gonna work with, it is not just a matter of habit.

ChizChiz

No man,

You are wrong.

 

List<Account> l1;
Account [] l2;
		
l1.add(new Account());
l2.add(new Account());
l1.get(0);
l2.get(1);
l1.clear();
l2.clear();

 

Give as an example of method which can NOT be invoked on ARRAY. As you see from me example above there is no such method which you can invoke on Lis tand can not on array.

Starz26Starz26
List can be multidimensional

A "array notation" of a list using [] can only be one dimensional
This was selected as the best answer
ChizChiz

You are absolutely right.

QasSFDCQasSFDC
one more difference I read on 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm

String[] colors = new List<String>();
String[] colors = new String[1];

Even though the size of the previous String array is defined as one element (the number between the brackets in new String[1]), 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.