0

I keep having an error like [D@7852e922 when trying to return an array in a method. Please look at my code. (Assignment: updates the Array by replacing all current entries with the value of a and updating it with the value of b.)(only change the method, not the header)

 public static void main(String[] args){
 double[] num = new double[]{1,2,4,9,-2,1.2};
 System.out.println(UpdateEntry(num,2,3));}
 public static double[] UpdateEntry (double[] array, int a, int b)
 {
 double[] newArray = new double[array.length];
 for (int j = 0; j < newArray.length; j++){
 if (array[j] == a){newArray[j] = b;}
 else {newArray[j] = array[j];}
 }
 return newArray;
 }

The output:

[D@7852e922

1 Answers1

-1

just replace this

System.out.println(UpdateEntry(num,2,3));

with

System.out.println(Arrays.toString(UpdateEntry(num,2,3));

here the doc

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(double[])


this is a way how to manually doit (taken from the official jdk6)

public static String toString(double[] a) {

    if (a == null)

        return "null";

    int iMax = a.length - 1;

    if (iMax == -1)

        return "[]";


    StringBuilder b = new StringBuilder();

    b.append('[');

    for (int i = 0; ; i++) {

        b.append(a[i]);

        if (i == iMax)

            return b.append(']').toString();

        b.append(", ");

    }

}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97