APEX BASICS : ARRAY [ ]

salesforce apex array by salesforce kid

APEX : ARRAY []

In the previous Episode, we have discussed how to call apex methods in a visualforce page.
From this episode, we are gonna discuss about more basic apex concepts in detail.
So let's get started.
ARRAY [] :


An array is a collection of similar elements, where the memory is allocated sequentially.
This array is similar to the array which we are using in another programming language.

Let's start with a declaration of apex array in Salesforce.

There are two ways different ways of Array declaration :
  • 1. Static Declaration
  • 2. Dynamic Declaration

1. Static Declaration 
=============================================
DataType arrayname = new DataType[] {value 1, value 2};
=============================================

Example :
  • Integer[] marks =new Integer[] {10,20,30};
  • String[] s1 = new String[] {'ram', 'sam', 'jack'}; 
2. Dynamic Declaration
=============================================
DataType[] arrayname = new DataType[size];
=============================================

Example :
  • String s1 = new String[2];
    s1[0] = 'kumar';
    s1[1] = 'ravi';
  • Integer a1 = new Integer[3];
    a1[0] = 25;
    a2[1] = 50;
    a3[2] = 75;
Now let's create one apex class by using an array of strings and display in a visualforce page.
So let's cook this recipe

Apex Class :
=============================================
public class ArrayExample
{
  public string[] myval {get; set;} //Declaration of getter and setter method with Array of string type
 public string name {get; set;}
 public ArrayExample() // Constructor Declaration
 {
   name = 'prasad';
   myval = new string[] {'sam', 'ram', 'jack'};
 }
}
=============================================

Now let's create visualforce page 

Visualforce Page :
=============================================
<apex:page controller="ArrayExample" >
 <apex:form>
  <apex:pageBlock>
   <apex:pageBlockTable value="{!myval}" var="a">

     <apex:column value="{!a}"/>

    </apex:pageBlockTable>

   <apex:outputlabel>{!name}</apex:outputlabel>

  </apex:pageBlock>
 </apex:form>
</apex:page>
=============================================

Now hit on preview


Output :
=============================================
salesforce apex array by salesforce kid

=============================================


Coool !!! The above output of visualforce displaying the array list present in apex class.

Now This is a basic example of using an array in apex class but, now let's take one more example with salesforce object for an example Account object.
Let's take to consider one more example to display an array of account records.

Apex Class :
=============================================
public class ArrayExample
{

 public Account[] abc {get; set;}

 public ArrayExample() //Declare values inside constructor 
 {
   Account a1 = new Account(name='ajinkya',industry= 'Banking');
   Account a2 = new Account(name='ravi',industry= 'Banking');
   Account a3 = new Account(name='yatish',industry= 'Banking');
   abc = new Account[]{a1,a2,a3};
  } 
}
=============================================

Now let's create a visualforce page and retrieve the value.

Visualforce Page :
=============================================
<apex:page controller="ArrayExample">
  <apex:form >
   <apex:pageblock >
    <apex:pageblocktable value="{!abc}" var="a">
     <apex:column value="{!a.name}"/>
      <apex:column value="{!a.industry}"/>
    </apex:pageblocktable>
  </apex:pageblock>
 </apex:form>
</apex:page>
=============================================

In the above code {!abc} will call abc from controller and store in variable a.

From {!a.name} you are requesting name from variable a.

From {!a.industry} you are requesting industry from variable a.

Finally, you will get a list of records present abc.

Hit on preview button.
Output :
=============================================

salesforce apex array by salesforce kid
=============================================

Coool !! Now you can easily use an array in apex class and retrieve the value wherever you want right ?
In the next episode, we will discuss apex collections like how to use a list, set, map.

WOHOOO !! YOU HAVE JUST COMPLETED APEX: ARRAY[] EPISODE 
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)

<< PREVIOUS                                                       NEXT >>

APEX BASICS : ARRAY [ ] APEX BASICS : ARRAY [ ] Reviewed by on Rating: 5

No comments:

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.