java

How to Convert ArrayList to Array in Java

In this tutorial, we are going to see how to convert ArrayList to Array in Java, by using toArray() method.
 

How to Convert ArrayList to Array in Java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		//Create an Arraylist
		List<String> languages = new ArrayList<>();
		
		//add string objects to the ArrayList
		languages.add("JAVA");
		languages.add("PHP");
		languages.add("PYTHON");
		languages.add("C++");

		System.out.println("Transform ArrayList into an array :");
		String[] arr = languages.toArray(new String[languages.size()]);
		//Display the array
		for(String i : arr){
			System.out.println(i);
		}
	}
}

Output:

Transform ArrayList into an array :
JAVA
PHP
PYTHON
C++
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *