Sei sulla pagina 1di 6

----------------------------------------------------------------Arrays :

---------------------------------------------------------------1. Array is a collection of simmilar elements


2. Memory for the array is allocated sequentialy
3. Elements in the array reffrered using index value
Syntax : DataType[] arrayName ;
Example : Integer[]
String []
Account[]
Contact[]
Student[]

ages;
names;
accs;
cons;
stds;

4 .Intilzation of memory for array.


Integer[] ages;
System.debug(age); // null
Note :array variable is a reference variable
Syntax :DataType[] arrayName=new DataType[size];
Example 1:
Integer[] ages=new Integer[4];
0
1
2
3
--------------------------------| null | null | null | null |
--------------------------------Example 2:
String[] names =new String[3];
0
1
2
---------------------------| null | null |
null |
----------------------------Example 3:
Account[] accs=new Account[4];
0
1
2
3
-----------------------------------| null | null | null | null |
-------------------------------------5. Assigning values for the Array
Example 1:
1. Integer[] ages=new Integer[4];
0
1
2
3
------------------------------

|null | null | null | null |


-----------------------------2. ages[0]=10;
0
1
2
3
---------------------------|10 | null | null | null |
---------------------------3. ages[3]=40 ;
0
1
2
3
---------------------------|10
| null | null | 40 |
---------------------------Example 2: Create array to names of city
1. String[] names=new String[4];
0
1
2
3
-----------------------------|null | null | null | null |
-----------------------------2. names[1]='Hyd';
0
1
2
3
-----------------------------|null | Hyd | null | null |
-----------------------------3.names[3]='Ban';
0
1
2
3
-----------------------------|null | null | null | Ban |
-----------------------------Example 3: Create a array to store account records ;
1. Account[] accs =new Account[3];
0
1
2
-----------------------------------------------|null
| null
| null
|
------------------------------------------------2. Account a1=new Account();
a1.name='Wipro';
a1.phone='123';
accs[0]=a1;
0
1
----------------------------------------------------------|Account:{name:wipro,Phone:123} |
|
-----------------------------------------------------------

3. Account a2=new Account();


a2.name='TCS';
a2.phone='345';
accs[1]=a2;
0
1 ------------------------------------------------------------|Account:{name:wipro,Phone:123} | Accoun:{Name:TCS,phone:345 |
--------------------------------------------------------------Example 3: Create a array of contact and insert two records
1.Contact[] cons=new Contact[3];
0
1
3
----------------------------------------------------------|
null
|
null
|
null
|
----------------------------------------------------------2. Contact c1=new Contact();
c1.lastname='Myla';
cons[0]=c1;
0
1
3
-------------------------------------------------------------|Contact:{lastname:Myla}|
null
|
null
|
--------------------------------------------------------------3. Contact c2=new Contact();
c2.lastname='M';
cons[1]=c2;
-------------------------------------------------------------|Contact:{lastname:Myla}|Contact:{lastname:m}| null
|
--------------------------------------------------------------6.Static Allocation of the elements in the array
Syntax: DataType[] arrayName=new DataType[]{val1,val2,val3};
Example 1:
Integer[] ages=new Integer[]{ 10,20,30};
0
1
2
--------------------------| 10 | 20 | 30 |
--------------------------Example 2:
String[] names=new String[]{ 'Sam','Ram','Kiran'};
0
1
2
---------------------------| Sam | Ram | Kiran |
----------------------------

Example 3:
Account a1=new Account();
a1.name='Wipro';
Account a2=new Account();
a2.name='TCS';
Account[] accs=new Account[]{a1,a2};
0
1
----------------------------------------------| Account:{Name:wipro} | Account: {Name: Tcs} |
----------------------------------------------================================================================
Reffering to all the elements in the array:
1. We can use for loop
Standard for loop :
for( Integer i=0; i< arrayname.size(); i++){
System.debug(arrayName[i]);
}
Example: Integer[] ages=new Integer[]{10,20,30,40};
0
1
2
3
----------------------------| 10 | 20 | 30 | 40 |
----------------------------Integer size=ages.size(); // size=4
for(Integer i=0;i< ages.size();i++){
System.debug(ages[i]);
}
For Loop : i=0 ; i< 4 ;i ++
System.debug(ages[i]); // Print 10
For Loop : i=1 ; i <4 ; i++
System.debug(ages[i]); // Print 20
For Loop : i=2; i< 4; i++
System.debug(ages[2]) ;// print 30
For Loop : i=3; i< 4 ;i++
System.debug(ages[3]); // print 40
For Loop : i=4; i<4 ;i++ : i<4 condition fail stop
Example 2:

String[] names=new String[]{'Hyd','Ban','Che' };


0
1
2
-----------------------| Hyd | Ban | Che |
----------------------Integer count=names.size(); // count=3
for(Integer i=0;i< names.size() ;i++){
System.debug(names[i]);
}
For Loop : i=0 ;i <3 ;i ++
Names[i] ; // Print Hyd
For Loop : i=1 ;i< 3 ; i++
Name[i] ;; // print Ban
For Loop : i=2 ;i< 3 ; i++
Name[i] ; // print Ban
For Loop : i=3 ;i< 3 ; i++ //condition fail
Example 3:
Account a1=new Account();
a1.name='TCS';
a1.phone='123';
Account a2=new Account();
a2.name='Wipro';
a2.phone='234';
Account[] accs=new Account[]{a1,a2};
0
1
----------------------------------------------------------------| Account:{name=TCS phone:123} | Account :{ name:wipr phone:234 |
-----------------------------------------------------------------Integer count=accs.size(); // 2
for(Integer i=0;i<accs.size(); i++){
System.debug(accs[i].Name);
System.debug(accs[i].phone);
}
For Loop : i=0
System.debug(accs[0].Name); // TCS
System.debug(accs[0].Phone); //123

For Loop : i=1


System.debug(accs[1].Name); //WIPRO
System.debug(accs[1].phone); // 234

Potrebbero piacerti anche