Try this:
int valorInicial = 3;
int n = 7;
ArrayList<Integer> lista = new ArrayList<>();
// usando math
for(int i = valorInicial; i <= n; i++){
lista.add((int)Math.pow(i,2));
}
for(Integer num : lista){
System.out.print(num + " ");
}
or without using the Math
class:
int valorInicial = 3;
int n = 7;
ArrayList<Integer> lista = new ArrayList<>();
//sem usar math
for(int i = valorInicial; i <= n; i++){
lista.add(i*i);
}
for(Integer num : lista){
System.out.print(num + " ");
}
In this ideone link, you can see the result (which will be the same) of the two working methods.