- Apex 编程教程
- Apex - 主页
- Apex - 概述
- Apex - 环境
- Apex - 示例
- Apex - 数据类型
- Apex - 变量
- Apex - 字符串
- Apex - 数组
- Apex - 常量
- Apex - 决策制定
- Apex - 循环
- Apex - 集合
- Apex - 类
- Apex - 方法
- Apex - 对象
- Apex - 接口
- Apex - DML
- Apex - 数据库方法
- Apex - SOSL
- Apex - SOQL
- Apex - 安全
- Apex - 调用
- Apex - 触发器
- Apex - 触发器设计模式
- Apex - 管理员限制
- Apex - 批处理
- Apex - 调试
- Apex - 测试
- Apex - 部署
- Apex 实用资源
- Apex - 快速指南
- Apex - 资源
- Apex - 讨论
Apex - 数组
Apex 中的数组基本上与 Apex 中的列表相同。数组与列表之间没有逻辑区别,因为它们的内部数据结构和方法也相同,但数组语法有点传统,类似于 Java。
以下是产品数组表示法 -
索引 0 - HCL
索引 1 - H2SO4
索引 2 - NACL
索引 3 - H2O
索引 4 - N2
索引 5 - U296
语法
<String> [] arrayOfProducts = new List<String>();
示例
假设,我们必须存储产品名称 - 我们可以使用数组,在数组中,我们将存储如下所示的产品名称。你可以通过指定索引来访问特定产品。
//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 the element in array
//We would access the element at Index 3
System.debug('Value at Index 3 is :'+arrayOfProducts[3]);
广告