简体   繁体   中英

Why do I have to use Array.toString at Split method?

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.toString call is necessary because String#split returns an array of Strings. You could, alternatively, iterate through the results, as follows:

String[] results = knights.split(regex);
for (String result : results) {
    System.out.println(result);
}

The formatting of the output would be slightly different, but it achieves the same idea. But as you can see, Arrays.toString is 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.

The reason why your program prints a memory address is because that is how the toString() method of an array behaves (and String.split() returns a String-array).

Why don't you want to use Arrays.toString()? It is not entirely clear what output you desire. If you just want another way for the same output you could do this:

String[] splitted = knights.split(regex);
List<String> list = Arrays.asList(splitted);
System.out.println(list);

When you use String.split() method it returns an array of String, which were initially separated based on your regex.

Array Class has overwritten toString() method so when you use it inside the System.out statement you were able to see all tokens.

An array is a special type of object in Java, it has to be handled differently by the compiler (eg indexing). It uses Object 's default toString() method, which only prints the address. An array ( String[] here) is NOT an instance of Arrays , which is only a helper class to work with arrays.

Try...

public static void split(String regex){
    String[] strArr = knights.split(regex); // Split the knights string using the specified delimiter
    // Cycle through the array
    for (String a : strArr) {
        System.out.print(a + " ");
    }
    System.out.println();
}

Pure arrays don't have a nice toString method like other objects in Java do (which explains why you got an address and not a list of elements). So, to print out the contents of the array, you will almost always will need to iterate through it using a for loop OR use the Array class' static toString method like you did in your first snippet, which pretty much does the same thing for you.

All in all, the implicit toString, such as System.out.println(someArray) , is not the same as Array.toString(someArray) .

See this API Doc for Arrays in which lies the utility method toString , you need to understand what it does when you pass it an array. And that's the method which is used in the "original code".

But in your code, you are calling a different toString method.

Way back in the days of Java 1.0, the .toString() method on arrays was the default implementation, so

knights.split(regex).toString()

would print out

[java.lang.String@01234abcdef]

or whatever (I can't remember the exact formatting). It was eventually decided that there should be a nice way to pretty-print arrays, but it was too late to change the behaviour or array.toString().

Thus, one of the static helper methods on java.util.Arrays is the new .toString(array) method, and that's why you can't just call .toString() on the array.

As other people have pointed out, if you really want to avoid Arrays.toString(), you could use a loop. But the library function is there to be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM