
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Is it possible to override toString method in an array using Java?
You can override the toString() method of the Object class but, if you are creating an object array of a particular class and want to print the contents of this array by overriding the toString() method instead of the, you cannot do that there is no solution for that in Java as of now.
But you can achieve this using various other methods −
Using the toString() method of the Arrays class
The toString() method of the Arrays class accepts a String array (in fact any array) and returns it as a String. Pass your String array to this method as a parameter. You can simply pass your array of objects to this method.
Example
import java.util.Arrays; class Student { String name = "Krishna"; int age = 20; Student(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Name: "+this.name+" "+"Age: "+this.age; } } public class Example { public static void main(String args[]) { Student std1 = new Student("Krishna", 20); Student std2 = new Student("Radha", 25); Student std3 = new Student("Trupthi", 30); Student std4 = new Student("David", 35); Student std5 = new Student("Moksha", 40); Student students[] = {std1, std2, std3, std4, std5}; System.out.println(Arrays.toString(students)); } }
Output
[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]
Using the asList() method of the Arrays class
This method accepts an array as an argument and, returns a List object. Use this method to convert an array to Set.
Example
import java.util.Arrays; class Student { String name = "Krishna"; int age = 20; Student(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Name: "+this.name+" "+"Age: "+this.age; } } public class Example { public static void main(String args[]) { Student std1 = new Student("Krishna", 20); Student std2 = new Student("Radha", 25); Student std3 = new Student("Trupthi", 30); Student std4 = new Student("David", 35); Student std5 = new Student("Moksha", 40); Student students[] = {std1, std2, std3, std4, std5}; System.out.println(Arrays.asList(students)); } }
Output
[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]
Using the ArrayList class
This is a bit different but solution −
Create a class extend the ArrayList class.
Add the objects to this class.
Use the toString() method of the ArrayList class to print the contents.
Example
import java.util.ArrayList; import java.util.Arrays; class Student { String name = "Krishna"; int age = 20; Student(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Name: "+this.name+" "+"Age: "+this.age; } } public class Example extends ArrayList<Object> { public static void main(String args[]) { Example obj = new Example(); obj.add(new Student("Krishna", 20)); obj.add(new Student("Radha", 25)); obj.add(new Student("Trupthi", 30)); obj.add(new Student("David", 35)); obj.add(new Student("Moksha", 40)); System.out.println(obj.toString()); } }
Output
[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]
- Related Articles
- Override the toString() method in a Java Class
- Is it possible to override a Java method of one class in same?
- Java toString() method.
- Is it possible to resize an array in C#
- ShortBuffer toString() method in Java
- Provider toString() method in Java
- Duration toString() method in Java
- MonthDay toString() Method in Java
- Instant toString() method in Java
- LocalDate toString() method in Java
- LocalDateTime toString() method in Java
- LocalTime toString() method in Java
- Is it mandatory to override the default methods of an interface in Java?
- Java Signature toString() method
- What is the purpose of toString() method? Or What does the toString() method do in java?
