0

I know how ot output an array as a string using Arrays.toString(). But then I have to set the method to return as a string.

a) how come when I run this:

public static void main(String[] args){
  print(myMethod(new int[]{1, 2, 3, 4}));
}

public static int[] myMethod(int[] nums){
    return nums;
}

I get [I@35a8767 ?

I'd like to know what the compiler is doing in the background when it spits that out.

and

b) is there a way to have it output correctly without having to make the method return as a String?

dbconfession
  • 1,147
  • 2
  • 23
  • 36
  • 2
    a) You're printing the address of the array in memory. b) Yes, program properly. – AntonH Feb 18 '14 at 21:23
  • the issue is that "correct" doesn't mean "print the contents of the array" – matt b Feb 18 '14 at 21:24
  • Because that's the default form for Object.toString. – Hot Licks Feb 18 '14 at 21:27
  • Hint: Look at the methods of java.util.Arrays. – Hot Licks Feb 18 '14 at 21:29
  • a) You're using the default `toString()` implementation of the `Object` class. b) Try: `System.out.println(Arrays.toString(nums));` – orlandocr Feb 18 '14 at 21:31
  • @orlandocr: I already use use System.out.println(Arrays.toString(nums)); That was my question. Is there a way to output it as an int[] instead of as a string without having it print garble? – dbconfession Feb 18 '14 at 21:54
  • @Hot Licks: am i missing something? The first line of my original post says that i know how to output using Arrays.toString(). I want to know if I can create a method that is supposed to return int[] and output the array legibly. Or do I have to use Arrays.toString()? – dbconfession Feb 18 '14 at 21:58
  • Yes, you're missing the fact that when you do println on an object there is an implicit call to toString that occurs. So what you see printed is the toString of an int array. – Hot Licks Feb 18 '14 at 22:01
  • @Hot Licks: never mind. I got it. You meant when I print the method from main(). I read too quickly. Thanks. System.out.println(Arrays.toString(myMethod(new int[]{1, 2, 3, 4}))); does the trick. But bottom line... there's not way to output an array in raw form without looping through each index of the array, correct? – dbconfession Feb 18 '14 at 22:24
  • I'm not sure what you mean by "raw form". The "raw form" of an int array is pretty much random (to the eye) bits and bytes. It must be somehow formatted to be visually meaningful. Arrays.toString is one way to do that. The other is to do it yourself. Those are your options. – Hot Licks Feb 18 '14 at 23:19
  • @Hot Licks: sorry. by "raw form" i mean without having to output it as a string. I guess the more I learn the more I realize there's no such thing as a legible version of an array. So I think I'm answering my own question. Ultimately, to see an array it has to be converted to a string, correct? – dbconfession Feb 19 '14 at 17:12
  • Correct. You can accept what `toString` returns (just data type and a sort of address), or you can accept what Arrays does, or you can write your own. – Hot Licks Feb 19 '14 at 17:16

0 Answers0