Java Program to Convert the ArrayList into a String and Vice Versa



In this article, we will understand how to convert the ArrayList into a string and vice versa. The ArrayList class is a resizable array, which can be found in the java. util package. The difference between a built-in array and an ArrayList in Java is that the size of an array cannot be modified.

ArrayList definition

An ArrayList in Java is a resizable array found in the java.util package, which allows for dynamic modifications in size as elements are added or removed.

  • It supports all optional list operations and allows null elements.
  • It provides methods to adjust the size of the underlying array used to store the list.
  • Its constant factor is lower than that of the LinkedList implementation.

Convert an ArrayList to a string

To convert an ArrayList to a string using the to String method. It creates an ArrayList, adds elements, and then converts it to a string. The following are the steps to convert an ArrayList to a string ?

  • Step 1: Initialize the ArrayList: We create an ArrayList and add some string elements to it using the add method.
ArrayList input_array = new ArrayList<>();
input_array.add("Java");
input_array.add("Python");
input_array.add("Scala");
input_array.add("JavaScript");
  • Step 2: Convert the ArrayList to a String: The toString method is used to convert the ArrayList into a string representation.
String result_string = input_array.toString();

Example 1

 The following is an example of converting an ArrayList to a string ?

import java.util.ArrayList; public class Demo {	public static void main(String[] args) {	System.out.println("The required packages have been imported");	ArrayList input_array= new ArrayList<>();	input_array.add("Java");	input_array.add("Python");	input_array.add("Scala");	input_array.add("JavaScript");	System.out.println("The array is defined as: " + input_array);	String result_string = input_array.toString();	System.out.println("\nThe result string is: " + result_string);	} }

Output

The required packages have been imported The array is defined as: [Java, Python, Scala, JavaScript] The result string is: [Java, Python, Scala, JavaScript]

Convert a string to an array

To convert a string into a character array we define a string, initialize an array of the same length, and then iterate over the string to store each character in the array. The following are the steps to convert a string to an array ?

  • Step 1: Define the String: We start by defining a string variable that we want to convert into an array.
String input_string = "Java Program";
System.out.println("The string is defined as: " + input_string);
  • Step 2: Initialize the Array: We create a character array of the same length as the input string to hold the characters.
char[] result_array = new char[input_string.length()];

  • Step 3: Convert String to Array: We use a loop to iterate over the string and store each character in the array.

for (int i = 0; i < input_string.length(); i++) {
result_array[i] = input_string.charAt(i);
}

Example 2

The following is an example of converting a string to an array. ?

public class Demo {	public static void main(String args[]) {	String input_string = "Java Program";	System.out.println("The string is defined as: " + input_string);	char[] result_array = new char[input_string.length()];	for (int i = 0; i < input_string.length(); i++) {	result_array[i] = input_string.charAt(i);	}	System.out.println("The array after conversion from string is: " );	for (char c : result_array) {	System.out.print(c + " ");	}	} }

Output

The string is defined as: Java Program The array after conversion from string is: J a v a P r o g r a m

Updated on: 2025-01-23T23:00:23+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements