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

前端 未结 7 565
一整个雨季
一整个雨季 2020-11-28 12:00

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 wh

相关标签:
7条回答
  • 2020-11-28 12:29

    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 :)

    0 讨论(0)
  • 2020-11-28 12:33

    Try this

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

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

    0 讨论(0)
  • 2020-11-28 12:34

    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.

    0 讨论(0)
  • 2020-11-28 12:47

    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?".

    0 讨论(0)
  • 2020-11-28 12:54

    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.

    0 讨论(0)
  • 2020-11-28 12:55

    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).

    0 讨论(0)
提交回复
热议问题