0

I have created a function which finds the maximum of groups of 3 in my array. When I come to print this, the memory address [I@43c46747 gets printed instead of the maximums which should be contained within the array.

I am not allowed to use any import statements/ anything advanced to solve this - trying to possibly use a helper function?

Thanks :)

EH24
  • 13
  • 2

2 Answers2

2

In fact, what you see is not a memory address, but array's class name and hash value (in hex form). This is a behavior inherited from toString() method of java.lang.Object

You can use a for-loop to print each array element individually.

eg.

for (int i = 0; i < myArray.length; i ++) {
    System.out.println(myArray[i]);
}

Note that println prints the result of toString method of the array elements for non-primitive types. Make sure their toString is properly overriden if you want custom behaviour rather than "classname@hashvalue"

Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70
0

When you try to print an Array normally, you are simply calling to toString() method on an Array object. In Java, this is the default implementation which prints out a result like you saw above [I@43c46747.

You should use

System.out.println(Arrays.toString(myArray));

to print meaningful values for an Array.

Kon
  • 10,702
  • 6
  • 41
  • 58