Arraylist java tostring – Java ArrayList toString() Method with Example

Arraylist java tostring: In the previous article we have discussed about Java ArrayList retainAll() Method with Example

In this article we are going to see the use of Java ArrayList toString() method along with suitable examples.

Java ArrayList toString() Method with Example

toString():

Java list tostring: This java.util.ArrayList.toString() method is used to convert an ArrayList into a String.

It returns the same ArrayList elements but in the form of String.

Syntax:

arrayListName.toString()

Where,

  • arrayListName refers to the name of your ArrayList.

Let’s see different examples to understand it more clearly.

Method-1: Java ArrayList toString() Method – Example with String Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then convert the ArrayLIst to an Array using toString() method.
  • Print the elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("rose");
        arr.add("lily");
        arr.add("marrygold");
        arr.add("jasmine");
        arr.add("tulip");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // convert ArrayList to String
        String str = arr.toString();
        System.out.println("String elements are: " +str);
    }
}
Output:

The elements of ArrayList are: [rose, lily, marrygold, jasmine, tulip]
String elements are: [rose, lily, marrygold, jasmine, tulip]

Method-2: Java ArrayList toString() Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then create an array without a parameter.
  • Then convert the ArrayList to an Array using toString() method.
  • Print the elements inside the Array using for each loop.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create an ArrayList of Integer datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // convert ArrayList to String
        String str = arr.toString();
        System.out.println("String elements are: " +str);
    }
}
Output:

The elements of ArrayList are: [2, 52, 13, 17, 1]
String elements are: [2, 52, 13, 17, 1]

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: