-5

I'm new in Java coding and I need some help.

I need a method to filter the squares numbers like( 4, 9, 16, 25) from an array, then save them in a new array without replacing the old one.

Unfortunately, I have no approach :/ .

here is some idea by myself:

public static int[] arrayFilter(int originalArray[]) {
    int[] filterArray = generateCopy(originalArray);
    for(int index = 0; index < filterArray.length; index++) {

        }
    }

}

I thank you for any help :)

K.Mac
  • 23
  • 2
  • 2
    Welcome to Stackoverflow. Please read [how-to-ask](https://stackoverflow.com/help/how-to-ask) and improve your question, help us help you. – Nir Alfasi May 09 '19 at 16:49
  • What did you try? what do you mean by a square? – MWB May 09 '19 at 16:49
  • I mean square numbers like (4, 9, 16, 25) – K.Mac May 09 '19 at 16:50
  • 2
    Please update the question – MWB May 09 '19 at 16:51
  • Please update your question with some things you've tried, or approaches you think might work. For example, how do you iterate over a list in Java? Can you come up with some example code and add it to your question? It doesn't have to be complete. – CalumMcCall May 09 '19 at 16:55
  • 2
    Possible duplicate of [Fastest way to determine if an integer's square root is an integer](https://stackoverflow.com/q/295579/5221149) – Andreas May 09 '19 at 17:01

2 Answers2

0

What about this, you iterate over the array and check that the square of the square root is the same as the original number. If so, you add it to a list that you convert to an array once you are done.

int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9,10 };
List<Integer> resultList = new ArrayList<>();
for (int number: numbers) {
    int sqrRoot = (int) Math.round(Math.sqrt(number));
    if (sqrRoot * sqrRoot == number) {
        resultList.add(number);
    }
}

or if you cannot use a List (see comment of author of the original question, below)...

int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9,10 };
int[] initResult = new int[numbers.length];
int numSquares = 0;
for (int i = 0; i < numbers.length; i++) {
    int sqrRoot = (int) Math.round(Math.sqrt(numbers[i]));
    if (sqrRoot * sqrRoot == numbers[i]) {
        initResult[numSquares] = numbers[i];
        numSquares++;
    }
}
int[] result = new int[numSquares];
for (int i = 0; i < numSquares; i++) {
    result[i] = initResult[i];
}

The reason I calculate the square root as follows (int) Math.round(Math.sqrt(numbers[i])) is to prevent floating point arithmetic issues. This way, the square root of the integer 4 will always be an integer with value 2.

MWB
  • 1,830
  • 1
  • 17
  • 38
  • Thanks a lot! But unfortunately I do not have the permission to use lists – K.Mac May 09 '19 at 16:59
  • In that case you just initialize an array to store the results and store the numbers in this array. The only thing is that you don't know the size of the resulting array, so you have to convert this array, yet again, to a new array once you are done and you know how many squares there are. – MWB May 09 '19 at 17:01
  • Thank you that was the solution! :) – K.Mac May 09 '19 at 17:36
-1

Here an example using Streams with a filter to create a new output array.

public class Squares {
    public static void main(String... args) {
        double[] numbers = {1, 2, 3, 4, 4.4, 100, 2500, 2500.1};
        double[] squares = Arrays.stream(numbers)
                .filter(Squares::isSquare)
                .toArray();
        System.out.println(Arrays.toString(squares));
    }

    private static boolean isSquare(double d) {
        double sr = Math.sqrt(d);
        return sr - Math.floor(sr) == 0;
    }
}
Jochen Reinhardt
  • 833
  • 5
  • 14
  • This may actually not work, I think, as you can run into problems of controlling the precision of floating point arithmetic. In case of 4, `sr` could initially result in 3.9999999. That means you will return `false` as `3.999999 - 3 != 0` – MWB May 09 '19 at 17:12