1

I am trying to print an array of some prime numbers which are divided by a number, the output I get for this code:

[J@15db9742

How can I print the array I get by the function.

public static long[] primes(int n) {
    int count = 0;
    long arr1[] = new long[n];
    for (int i = 2; i < n; i++) {
        boolean prime = true;
        if (n % i == 0)
            for (int j = 2; j < i && prime; j++) {
                if (i % j == 0) prime = false;
            }
        if (prime && n % i == 0) {
            arr1[count] = i;
            System.out.println(arr1[count]);
            count++;
        }
    }

    long arr[] = new long[count];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = arr1[i];
    }
    return arr;
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
oprezyzer
  • 101
  • 11

2 Answers2

4

Array is reference type.

And you have to use utility class Arrays for printing arrays:

Arrays.toString(array)

for multidimensional arrays:

Arrays.deepToString(array)

BTW
Small suggestions you can use some optimization instead for loop:

    for (int i = 0; i < arr.length; i++) {
        arr[i] = arr1[i];
    }

better to use System.arraycopy():

System.arraycopy(arr1, 0, arr, 0, arr.length);

catch23
  • 17,519
  • 42
  • 144
  • 217
0
for(long n: arr)
 System.out.println(n);
Soham
  • 4,397
  • 11
  • 43
  • 71