-1

i have a string var, give it an example

String s = "just\nan\nexample";

i want to split that string into string array by using line space(\n) as delimiter, so the output string array will look like

String[] array = [just, an, example];

so then i tried this code

String[] array = s.split("\n");
System.out.println(array);

it gave me this output in the console

[Ljava.lang.String;@691aca

what's going on? am i using this method wrong?

EDIT: Okay, it's answered.. this is one of the answer

System.out.println(java.util.Arrays.toString(array));

but this make me questioned about something, i also have another string array which i put on the value from a txt file using bufferedreader loop and it can printed out array list seamlessly without using "java.util.Arrays.toString(array)"

but why when i use an array which i value i put on manually (as above) or get from another variable, i can't get it printed out properly without using "java.util.Arrays.toString(array)"?

MIMB
  • 1
  • 4
  • @Boris No, I was too fast. So I did not lie I just was wrong. – Fildor Sep 20 '14 at 09:09
  • arrays do not have toString methods built in – Gerard Sep 20 '14 at 09:09
  • 1
    There's also [this duplicate](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-an-array). And probably a hundred others. – Boris the Spider Sep 20 '14 at 09:10
  • Use `java.util.Arrays.toString(array)` – René Link Sep 20 '14 at 09:10
  • i'm sorry if this is duplicate, but i want ask something. i also have another string array which i put on the value from a txt file using bufferedreader loop and it can printed out array list seamlessly without using "java.util.Arrays.toString(array)" but why when i use an array which i value i put on manually (as above) or get from another variable, i can't get it printed out properly without using "java.util.Arrays.toString(array)"? – MIMB Sep 20 '14 at 09:32
  • You say _array list seamlessly_; so you mean it's an `ArrayList`? Then you have your answer. – Boris the Spider Sep 20 '14 at 09:34
  • oh i see, so if it's array type, it can't use println function properly without java.util.Arrays.toString(array).. thanks – MIMB Sep 20 '14 at 09:39
  • The thing is array itself is an object, but despite other objects, it has no toString() method, which you may override to have a string presentation. And Arrays.toString() simply iterates throught all objects in your array and add it to StringBuffer, and finally you have stringBuffer.toString(). – Mykola Evpak Sep 20 '14 at 10:00

1 Answers1

0

That's because your printing the address of the array. Try looping through the array and get each element like this:

String[] array = s.split("\n");
for (String str : array) {
    System.out.println(str);
}
lxcky
  • 1,668
  • 2
  • 13
  • 26