- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to convert an array to a list
The Arrays class of the java.util package provides a method known as asList(). This method accepts an array as an argument and, returns a List object. To convert an array to a List object −
- Create an array or read it from the user.
- Using the asList() method of the Arrays class convert the array to a list object.
- Print the contents of the List object.
Example
import java.util.Arrays; import java.util.List; import java.util.Scanner; public class ArrayToList { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array to be created ::"); int size = sc.nextInt(); String [] myArray = new String[size]; for(int i=0; i<myArray.length; i++){ System.out.println("Enter the element "+(i+1)+" (String) :: "); myArray[i]=sc.next(); } List<String> list = Arrays.asList(myArray); System.out.println("Given array is converted to a list"); System.out.println("Contents of list ::"+list); list.toArray(myArray); } }
Output
Enter the size of the array to be created :: 4 Enter the element 1 (String) :: Java Enter the element 2 (String) :: JavaFX Enter the element 3 (String) :: WebGL Enter the element 4 (String) :: JoGL Given array is converted to a list Contents of list ::[Java, JavaFX, WebGL, JoGL]
- Related Articles
- Java program to convert a list to an array
- How to convert an Array to a List in Java Program?
- Program to convert Array to List in Java
- How to convert an array to a list in Java?
- Java program to convert a Set to an array
- Golang Program to Convert Linked list to an Array
- Can we convert a list to an Array in Java?
- Java program to convert an Array to Set
- Java Program to convert a set into an Array
- Program to convert Stream to an Array in Java
- Write a program to convert an array to String in Java?
- convert list to array in java
- How to convert a list to array in Java?
- Can we convert a Java array to list?
- Can we convert a Java list to array?

Advertisements