-1
public class Practise
{
    public static void main(String[] args)
    {
        double[] array = {1.0,2.0,2.0,4.0};
        System.out.println(square(array));
    }
    public static double[] square (double[] a)
    {
        double [] squared = new double[a.length];
        for (int i = 0; i <a.length ; i++)  {
            squared[i] = a[i] * a[i];
        }
        return squared;
    }

I'm trying to update squared array with the result of a array's values squared and then return it but I'm keep getting an [D@1540e19d error , sorry if it is a easy question I am a student just started learning java!`

  • 4
    Possible duplicate of [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Tom May 29 '17 at 15:22

5 Answers5

0

You are printing the array object reference, you should print each cell of your array, try:

public class Practise
{
    public static void main(String[] args)
    {
        double[] array = {1.0,2.0,2.0,4.0};
        double[] result = square(array);

        for(int i = 0; i < result.length; i++)
        {
          System.out.println("Square of " + array[i] + " is: " + result[i]);
        }

    }


    public static double[] square (double[] a)
    {
        double [] squared = new double[a.length];
        for (int i = 0; i <a.length ; i++)  {
            squared[i] = a[i] * a[i];
        }
        return squared;
    }
}
storm87
  • 49
  • 1
  • 5
0

You need to make some modifications in your code: For case 1:

public class Practise {

    public static void main(String[] args) {
        double[] array = {1.0, 2.0, 2.0, 4.0};
        array = square(array); //to get new array from method
        for(int i=0; i<array.length; i++)
            System.out.println(array[i]); // print array by index
    }

    public static double[] square(double[] a) {
        double[] squared = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            squared[i] = a[i] * a[i];
        }
        return squared;
    }
}

OUTPUT:

1.0
4.0
4.0
16.0

For case 2:

import java.util.Arrays;

public class Practise {

    public static void main(String[] args) {
        double[] array = {1.0, 2.0, 2.0, 4.0};
        System.out.println(Arrays.toString(square(array)));
    }

    public static double[] square(double[] a) {
        double[] squared = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            squared[i] = a[i] * a[i];
        }
        return squared;
    }
}

OUTPUT:

[1.0, 4.0, 4.0, 16.0]

In the case 1 you can control and handle the output of the new array

Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
0

Instead of

  System.out.println(square(array));

use :

System.out.println(Arrays.toString(square(array)));

and see What's the simplest way to print a Java array?

NB : D@1540e19d is not a error.It's the hashCode() in hexadecimal. See http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--

infox09
  • 5
  • 4
0

There is no error in your program. You are printing the memory address of array, which is [D@1db something like this. it is vary(because it is memory address).

Change:

System.out.println(square(array));

To:

 System.out.println(square(array)[0]);

tihs will only print index 0 value. If you want to print all the values in array you can do like this:

for(Double d: square(array)){
    System.out.println(d);
}

Here I have used for-each(enhanced for loop). If you are not familiar with this go with for loop.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

If I were you, I would create another array that stores the squared values ofdouble[] array = {1.0, 2.0, 3.0, 4.0}; . I don't think you need double[] squaredinside the method. Just use the one that you have given in the parameterlist.

public class square {
public static void main (String[] args){
    double[] array = {1.0, 2.0, 3.0, 4.0};
    double[] array1 = squareArray(array);
    for(double elem: array1){
        System.out.println(elem);
    }
}
public static double[] squareArray(double[] a){
    for(int i = 0; i < a.length; i++){
        a[i] *= a[i];
    }
    return a;
}
}

Array1 stores the squared values of array. I'm not quite sure what you're question is, but if it's about just printing out the array, the for-each-loop is a must-know for a beginner programmer. I've written it in the code. I'd strongly advice you to start using it and undertand its functionality. Hope this was helpful.

nasdenkov
  • 54
  • 10