0

If I create an array of objects and I want to print the array is possible to give him a String value as representation in the array?

Example:

If I create a 2D array [7][7] and I put an object called Car in position [1][5] when I print the array is possible to give a String value of "Toyota" in the array? I want that the object appear in the array as a Toyota

  • 4
    Override the `toString()` method in the `Car` class. – MC Emperor Mar 02 '20 at 16:40
  • 1
    Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – M. Prokhorov Mar 02 '20 at 16:41
  • @MCEmperor could you explain me more about `toSting()` ? – Kevin Nathaniel Tiscareno Mar 02 '20 at 16:45
  • @M.Prokhorov not because I have a class Car that have a lot of methods, so I want to put it in my 2d array but when I print the array I want that the array show as Toyota in the specific place. You could see it if I was creating a chess game and I create an object of every piece, then I put it into an array but I would want to have a way to recognize which object is in which place. – Kevin Nathaniel Tiscareno Mar 02 '20 at 16:49
  • Here is a SO question about method `toString()`. [How to override toString() properly in Java?](https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java) – Abra Mar 02 '20 at 17:11
  • @KevinNathanielTiscareno, and you want this just in array? Then it's not possible directly, you will need custom printer. Otherwise see my link and the one Abra added. – M. Prokhorov Mar 02 '20 at 17:51

0 Answers0