function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BhavyaBhavya 

Array declaration in Apex

i ran the below program in Apex  but i am getting error so please could you help me.

 

public  class SimpleArrayClass
{public static  integer findSum(Integer[]a)
{
Integer s=0;
for(integer x:a)
{s=s+x;
  }
 
 
return s;
}

  }

--------------------

 

@isTest
public class SimpleArrayTestClass
{
 @isTest Static Void sumTest()
{
Integer [] a=new Integer[4]{10,20,30,40};
integer act=0,exp=100;
act=SimpleArrayClass.findsum(a);
system.debug('Actual Value...' +act);
}
}

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

update your test class to

 

@isTest
public class SimpleArrayTestClass
{
 Static testmethod Void sumTest()
{
Integer [] a=new Integer[]{10,20,30,40};
integer act=0,exp=100;
act=SimpleArrayClass.findsum(a);
system.debug('Actual Value...' +act);
}
}

BhavyaBhavya

I am getting below error once i ran  your code

 

Error: Compile Error: unexpected token: '{' at line 18 column 27

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Not sure on how you are running your code...

 

Step 1:

Create a new class...

 

public  class SimpleArrayClass
{

public static  integer findSum(Integer[]a)
{
Integer s=0;
for(integer x:a)
{

  s=s+x;
}
return s;
}
}

 

Step 2: Create another class with the below code..

 

@isTest
public class SimpleArrayTestClass
{
 Static testmethod Void sumTest()
{
Integer [] a=new Integer[]{10,20,30,40};
integer act=0,exp=100;
act=SimpleArrayClass.findsum(a);
system.debug('Actual Value...' +act);
}
}

 

 

Then once you save the above class... click on run test to see the coverage..

 

Then open your developer console (Click on your name on top right hand corner-> Developer console)

copy paste the below code..

 

Integer [] a=new Integer[]{10,20,30,40};
integer act=0,exp=100;
act=SimpleArrayClass.findsum(a);
system.debug('Actual Value...' +act);

 

click execute... and then click on open Raw Log... you will see Actual value as 100.

 

Just tested and all works fine.. not sure what exactly u doing.

 

 

BhavyaBhavya

it's working fine now,

Thanks for your help.

 

Prem ChauhanPrem Chauhan
HI Bhavya,

here is the complete tutorial for ARRAYS just go through once 
ARRAYS IN SALESFORCE – APEX TUTORIAL (EXAMPLES)

Even you get complete Developer Guide (http://www.salesforceupdates.com/category/salesforce/developer/) here just have a look and learn quickly (Salesforce)

Thanks :)