Java: Convert an array to ArrayList
Java Array: Exercise-20 with Solution
Write a Java program to convert an array to an ArrayList.
Pictorial Presentation:

Sample Solution:
Java Code :
// Import the ArrayList and Arrays classes from the Java utility library.
import java.util.ArrayList;
import java.util.Arrays;
// Define a class named Exercise20.
public class Exercise20 {
public static void main(String[] args) {
// Create an array of strings.
String[] my_array = new String[] {"Python", "JAVA", "PHP", "Perl", "C#", "C++"};
// Create an ArrayList of strings and initialize it with the contents of the array.
ArrayList list = new ArrayList(Arrays.asList(my_array));
// Print the ArrayList to the console.
System.out.println(list);
}
}
Sample Output:
[Python, JAVA, PHP, Perl, C#, C++]
Flowchart:

Java Code Editor:
Previous: Write a Java program to add two matrices of the same size.
Next: Write a Java program to convert an ArrayList to an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join