Java ArrayList toString() Method
What exactly is an ArrayList?
ArrayList gives us resizable arrays. Because ArrayLists are resizable, we can add or remove elements at any time. It is not required to mention size when declaring it. The ArrayList class is included in the Java framework and can be found in the java.util package. ArrayList has many built-in methods that we can use.
This page will cover the Java ArrayList toString() method.
Java ArrayList toString() Method
In Java, all classes are directly or indirectly subclasses of object classes. The toString() method is in the object class. The toString() method converts an array list to a string. Let’s see how to use this method, its syntax, parameters and return value with an example.
Syntax :
ArrayList.toString();
Parameters:
This method does not take any parameters.
Return values :
It returns string representing the arraylist.
ArrayList toString() Method
Example :
import java.util.ArrayList; class Main { public static void main (String[]args) { // creating an Integer ArrayList ArrayList < String > list = new ArrayList <> (); // adding elements in the list list.add ("asd"); list.add ("qwe"); list.add ("qwed"); list.add ("qaz"); list.add ("wsdc"); // convert arraylist to string using toString() method String result = list.toString (); // printing the string System.out.println ("The new string is : " + result); } }
Output :
The new string is : [asd, qwe, qwed, qaz, wsdc]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment