I don’t know why I have to use Array.toString in the split(String reges) method. I have tried that if I leave the Array.toString() method, but after that the method writes out only the memory address.
This is the original code:
public static String knights=
"Then, when you have found the shrubbery, you must " +
"cut down the mightiest tree in the forest... " +
"with... a herring!";
public static void split(String regex){
System.out.println(Arrays.toString(knights.split(regex)));
}
And I have tried this:
public static void split(String regex){
System.out.println(knights.split(regex).toString());
}
But it writes out only the address, and didn’t the contain of knights.
Or how can I rewrite the split method without use the Arrays.toString() method?
The
Arrays.toStringcall is necessary becauseString#splitreturns an array of Strings. You could, alternatively, iterate through the results, as follows:The formatting of the output would be slightly different, but it achieves the same idea. But as you can see,
Arrays.toStringis a bit more concise, and requires less effort on your part. The iteration method (above) gives you more control over the formatting of the output, however.