Easy Tutorial
❮ Java String Equalsignorecase Java9 Completablefuture Api Improvements ❯

Java ArrayList toString() Method

Java ArrayList

The toString() method converts an ArrayList object to a string.

The syntax for the toString() method is:

arraylist.toString()

Note: arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

Returns the string representation of the arraylist.

Example

Converting an ArrayList to a String type:

Example

import java.util.ArrayList;
import java.util.Comparator;
class Main {
    public static void main(String[] args){

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();

        sites.add("tutorialpro");
        sites.add("Google");
        sites.add("Wiki");
        sites.add("Taobao");
        System.out.println("Website List: " + sites);

        // Convert ArrayList to String type
        String list = sites.toString();
        System.out.println("String: " + list);
    }
}

Executing the above program outputs:

Website List: [tutorialpro, Google, Wiki, Taobao]
String: [tutorialpro, Google, Wiki, Taobao]

In the above example, we created an array named sites.

Note this line:

String list = sites.toString();

We used the toString() method to convert the arraylist to a string, which converts the entire arraylist into a String type.

Note: The ArrayList class does not have its own toString() method; it overrides the toString() method of the Object class.

Java ArrayList

❮ Java String Equalsignorecase Java9 Completablefuture Api Improvements ❯