I have a simple question:

I have a loop that iterate on an integer i.

each loop iteration i becomes i+1

I have another set of variables : x1, x2, x3, x4 etc

I would like at each iteration of the loop to do something like :

x+i = i

The goal is therefore to have x1=0 x2=1 x3=2 ...

When I do x+i=i I have an error

What would be the correct call to have the right result

1 Answer 1


Make "x" an array indexed by "i" and do:

Integer[] x = new Integer[maxI];

for (Integer i = 0; i < maxI; i++) {
    x[i] = i;

You could, of course, ignore x[0] and do x[i + 1] = i instead to better align with your x1 = 0, x2 = 1 etc.

  • Thank you so much. That was helpful @PhilW and then if I want to replace it later on in a body with : body = body.replace('{!x[i]},x[i]) will that work out or should I do another loop ?
    – Tiburce
    Jul 3, 2019 at 9:51
  • Not quite sure what you're doing here. Note that x[i] will (at least initially and if you don't actually modify the value after you first set up the array) necessarily be the same as i. If you're in a Visualforce page you can use apex:repeat to iterate. If you're in Apex you'd use a for loop like "for (Integer value : x)" to iterate across all the entries.
    – Phil W
    Jul 3, 2019 at 10:15
  • Thank you @PhilW, I will create a new post to explain properly my implementation problem but it is nice to see members like you here. ;)
    – Tiburce
    Jul 3, 2019 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .