
- 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
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.
Below is a demonstration of the same −
Suppose our input is −
Input string: Java Program
The desired output would be −
The array after conversion from string is: J a v a P r o g r a m
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create an array and add elements to it using the ‘add’ method. Step 5 - Display the list on the console. Step 6 - Create another empty array of previous array size. Step 7 - Convert it into string using the ‘toString’ method. Step 8 - Iterate over the elements and display the elements on the console. Step 9 - Stop
Example 1
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]
Example 2
Here, we convert 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
- Related Articles
- Golang program to convert the arraylist into string and vice-versa
- Java Program to Convert the LinkedList into an Array and vice versa
- How to convert String to StringBuilder and vice versa Java?
- How to get ArrayList to ArrayList and vice versa in java?
- Golang program to convert a linked list into an array and vice-versa
- How to Convert a String to Hexadecimal and vice versa format in java?
- Convert string to DateTime and vice-versa in Python
- Java Program to Convert contents of a file to byte array and Vice-Versa
- C program to convert upper case to lower and vice versa by using string concepts
- C++ Program to Convert Binary Number to Octal and vice-versa
- C++ Program to Convert Binary Number to Decimal and vice-versa
- C++ Program to convert Octal Number to Decimal and vice-versa
- C++ Program to Convert Km/hr to miles/hr and vice versa
- How to convert an array to Set and vice versa in Java?
- How can you convert the saturated solution into unsaturated or vice-versa?

Advertisements