English 中文(简体)
Apex - Arrays
  • 时间:2024-12-22

Apex - Arrays


Previous Page Next Page  

Arrays in Apex are basically the same as Lists in Apex. There is no logical distinction between the Arrays and Lists as their internal data structure and methods are also same but the array syntax is pttle traditional pke Java.

Below is the representation of an Array of Products −

Index 0 − HCL

Index 1 − H2SO4

Index 2 − NACL

Index 3 − H2O

Index 4 − N2

Index 5 − U296

Syntax

<String> [] arrayOfProducts = new List<String>();

Example

Suppose, we have to store the name of our Products – we can use the Array where in, we will store the Product Names as shown below. You can access the particular Product by specifying the index.

//Defining array
String [] arrayOfProducts = new List<String>();

//Adding elements in Array
arrayOfProducts.add( HCL );
arrayOfProducts.add( H2SO4 );
arrayOfProducts.add( NACL );
arrayOfProducts.add( H2O );
arrayOfProducts.add( N2 );
arrayOfProducts.add( U296 );

for (Integer i = 0; i<arrayOfProducts.size(); i++) {
   //This loop will print all the elements in array
   system.debug( Values In Array:  +arrayOfProducts[i]);
}

Accessing array element by using index

You can access any element in array by using the index as shown below −

//Accessing the element in array
//We would access the element at Index 3
System.debug( Value at Index 3 is : +arrayOfProducts[3]);
Advertisements