Apex Programming Tutorial
Apex Useful Resources
Selected Reading
- Apex - Deployment
- Apex - Testing
- Apex - Debugging
- Apex - Batch Processing
- Apex - Governer Limits
- Apex - Trigger Design Patterns
- Apex - Triggers
- Apex - Invoking
- Apex - Security
- Apex - SOQL
- Apex - SOSL
- Apex - Database Methods
- Apex - DML
- Apex - Interfaces
- Apex - Objects
- Apex - Methods
- Apex - Classes
- Apex - Collections
- Apex - Loops
- Apex - Decision Making
- Apex - Constants
- Apex - Arrays
- Apex - Strings
- Apex - Variables
- Apex - Data Types
- Apex - Example
- Apex - Environment
- Apex - Overview
- Apex - Home
Apex Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apex - Arrays
Apex - Arrays
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