Is it possible in Java to override 'toString' for an Objects array?

大兔子大兔子 提交于 2019-11-26 08:24:49

问题


Is it possible in Java to override a toString for an Objects array?

For example, let\'s say I created a simple class, User (it doesn\'t really matter which class is it since this is a general question). Is it possible that, once the client creates a User[] array and the client uses System.out.print(array), it won\'t print the array\'s address but instead a customized toString()?

PS: of course I can\'t just override toString() in my class since it\'s related to single instances.


回答1:


No. Of course you can create a static method User.toString( User[] ), but it won't be called implicitly.




回答2:


You can use Arrays.toString(Object[] a); which will call the toString() method on each object in the array.

Edit (from comment):

I understand what it is you're trying to achieve, but Java doesn't support that at this time.

In Java, arrays are objects that are dynamically created and may be assigned to variables of type Object. All methods of class Object may be invoked on an array. See JLS Ch10

When you invoke toString() on an object it returns a string that "textually represents" the object. Because an array is an instance of Object that is why you only get the name of the class, the @ and a hex value. See Object#toString

The Arrays.toString() method returns the equivalent of the array as a list, which is iterated over and toString() called on each object in the list.

So while you won't be able to do System.out.println(userList); you can do System.out.println(Arrays.toString(userList); which will essentially achieve the same thing.




回答3:


You can create a separate class containing the array, and override toString().

I think the simplest solution is to extend the ArrayList class, and just override toString() (for example, UserArrayList).




回答4:


The only way you can do this is to re-compile Object.toString() and add instanceof clauses.

I had requested a change in Project Coin to handle arrays in a more object orientated way. I felt that it's too much for beginners to learn all the functionality you need in Array, Arrays and 7 other helper classes which are commonly used.

I think in the end it was concluded that to make arrays properly object orientated is a non-trivial task which will be pushed back to Java 9 or beyond.




回答5:


Try this

User[] array = new User[2];
System.out.println(Arrays.asList(array));

of course if you have customized user.toString() method




回答6:


You cannot do that. When you declare an array, then Java in fact creates a hidden object of type Array. It is a special kind of class (for example, it supports the index access [] operator) which normal code cannot directly access.

If you wanted to override toString(), you would have to extend this class. But you cannot do it, since it is hidden.

I think it is good to be it this way. If one could extend the Array class, then one could add all kinds of methods there. And when someone else manages this code, they see custom methods on arrays and they are "WTF... Is this C++ or what?".




回答7:


Well, you can try to wrap all calls to Object.toString with an AspectJ around advice and return the desired output for arrays. However, I don't think it's a good solution :)



来源:https://stackoverflow.com/questions/10295600/is-it-possible-in-java-to-override-tostring-for-an-objects-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!