Showing posts with label arrays in salesforce. Show all posts
Showing posts with label arrays in salesforce. Show all posts

Sunday, 9 July 2017

Difference between Arrays and collections

Difference between Arrays and collections

Arrays 
      
1) Array is a collection of homogeneous (similar) elements   
2) Arrays cannot grow and shrink dynamically.
3) Arrays can be accessed faster and less memory.

Collections  
  
1) It is a collection of homogeneous and heterogeneous elements
2) It can grow and shrink dynamically.
3) Collections are slow compared to arrays and consume more memory.

Arrays in Salesforce Apex

Arrays in Salesforce Apex 

Array: 

Array is a collection of similar elements, where the memory allocated sequently. 

Data type [] array name = New data type (size); 

// this is called dynamic declaration   

Data type [] array name = New data type [] {value; value 2};

// static declaration 

         Integer [] marks = new integer [] {10,20,30}; 
         Account a1 = new account (name=’same’);
         Account a2 = new account (name=’ram’); 
         Account [] acc = new account [] {a1, a2}; 
         String [] s1 = new string [] {‘ram’, ‘Sam’, ‘ram’};
        String [] s1 new string [4]; 
            S1[0] = ‘Kumar’; 
            S1 [1] = ‘Ravi’;