• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can we implement toString() for arrays.  RSS feed

 
kannaya radhe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might seem to be a silly question. But I really want to know it.

When we write ...

>>> Student s = new Student("Roll No", "Name");
>>> System.out.println(s);

Then it gives output accroding to the way that toString() of Student is implemented,right ?
What if I wanna do the same for arrays?? I mean to ask, let us say I have array int[] arr = { 1, 2, 3, 4, 5}. When I say System.out.println(arr), it should give the output as it gives for collections [1,2,3,4,5].
I know that Arrays.toString(int[]) can be of help us in this regard. But my question is that can we write our own toString() method so that when I print the "arr" (an Object itself) it should call the toString() that we implemented like a callback function.

Kindly help me in this regard.

Best Regards
Krishna
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are part of the language with their toString as provided.

You can roll out your own toString for classes you wrote.
You can wrap the array into a custom class with a toString as you like.
 
kannaya radhe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello

Sorry, I think I might not have explained my question clearly. My concern is to say that ...

1. When I say Student s = new Student(...,...); System.out.println(s);
>> As you know, this calls the toString() method of Object class if I don't override it in Student class, otherwise it will call the overridden method in Student class.

2. When I print any array like int[] arr = {1,2,3,4,5}. System.out.println(arr);
>> Now it will call the toString() methods of Object class(being Object is super class of an array) which prints something like this: [I@15db9742.

Now my question is that can't I implement the toString() method for an array so that when I print using System.out.println(arr) it prints the array like this[1,2,3,4,5] rather than [I@15db9742.
I know that java.util.Arrays.toString(int[]) will do. But is it possible to implement our own toString() for an array??

Best Regards
Krishna
 
kannaya radhe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

OK, I got it from your comment "You can wrap the array into a custom class with a toString as you like.".

Thank you for your clarification.

Best Regards
Krishna
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java® Language Specification says that the only overridden method in an array is clone() so you cannot directly implement toString on an array. You can however print out arrays nicely.
Two ways to do it, and there are many more:-
  • System.out.println(Arrays.toString(myArray));
  • /* Only works in Java8+ */
    StringJoiner joiner = new StringJoiner(", ", "ARRAY[", "]ARRAY");
    for (Foo f : myArray)
    {
       joiner.add(f);
    }
    System.out.println(joiner);


  • Note this method, too.
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The class can be an anonymous one:

     
    Campbell Ritchie
    Marshal
    Posts: 56530
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Possibly but shouldn't the array be a field of the anonymous class.
    I am not convinced that an anonymous class is the best solution for this particular problem.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!