Apex中的數(shù)組基本上與Apex中的列表相同。 在數(shù)組和列表之間沒有邏輯區(qū)別,因?yàn)樗鼈兊膬?nèi)部數(shù)據(jù)結(jié)構(gòu)和方法也是相同的,但是數(shù)組語法不像Java那樣是傳統(tǒng)的。
下面是產(chǎn)品數(shù)組( Array of Products )的表示:
Index 0 - HCL
Index 1 - H2SO4
Index 2 - NACL
Index 3 - H2O
Index 4 - N2
Index 5 - U296
語法:
<String> [] arrayOfProducts = new List<String>();
示例:
假設(shè),我們想存儲我們的產(chǎn)品的名稱,那么我們可以使用數(shù)組,其中我們可以存儲產(chǎn)品名稱如下所示。 您可以通過指定索引來訪問特定的產(chǎn)品。
//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]); }
使用索引訪問數(shù)組元素:
您可以使用索引訪問數(shù)組中的任何元素,如下所示:
//Accessing the element in array //We would access the element at Index 3 System.debug('Value at Index 3 is :'+arrayOfProducts[3]);
更多建議: