Tell me, how in a similar method to avoid duplication of code?

public static int[] findNumbers(int n) { int[] result = new int[(int) Math.sqrt(n)]; for (int i = 1; i * i < n; i++) { result[i - 1] = i * i; } return result; } 
  • With this method, everything is in order, there is no duplication. - Artem Konovalov
  • @ArtemKonovalov and here they say to me, that is, i * i, as I understand it, is duplicated. - Ilya Zhavoronkov
  • one
    int j = 0; for (int i = 1; (j = i * i) < n; i++) { result[i - 1] = j; } int j = 0; for (int i = 1; (j = i * i) < n; i++) { result[i - 1] = j; } heh ..... - Alexey Shimansky
  • 2
    Why i * i <n, if i can be compared with the size of the array, or save the value of the expression (int) Math.sqrt (n) - Ruslan P.
  • one
    As a variant of Alexey Shimansky’s modification for (int i = 1, j = 1; j <n; i ++, j = i * i) {result [i - 1] = j; } - Maxim Drobyshev

0