Created
December 24, 2021 08:21
-
-
Save gauravkukade/10983d14fc67fa9bf6c690258f1613cf to your computer and use it in GitHub Desktop.
A java program to convert an ArrayList to array using toArray() method. toArray(T[] a) method returns T[], Read more at https://coderolls.com/convert-arraylist-to-array/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
/** | |
* A java program to convert an ArrayList to array using | |
* toArray method. | |
* | |
* toArray(T[] a) method returns T[] | |
* | |
* @author Gaurav Kukade at coderolls.com | |
* | |
*/ | |
public class ArrayListToArray2 { | |
public static void main(String[] args) { | |
ArrayList<String> arrayList = new ArrayList<String>(); | |
//adding elements to the arrayList | |
arrayList.add("Meta"); | |
arrayList.add("Apple"); | |
arrayList.add("Amazon"); | |
arrayList.add("Netflix"); | |
arrayList.add("Microsoft"); | |
arrayList.add("Google"); | |
System.out.println("ArrayList: \n"+ arrayList); | |
// convert ArrayList to array | |
// pass a new String array of the arrayList size as a param to toArray | |
//String[] elements = arrayList.toArray(new String[arrayList.size()]); | |
//kindly read the 'Important Note' below | |
String[] elements = arrayList.toArray(new String[0]); | |
System.out.println("\nArray:"); | |
//we will receive the string array, so iterate on it to print each element | |
for(String str:elements) { | |
System.out.println(str); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList: | |
[Meta, Apple, Amazon, Netflix, Microsoft, Google] | |
Array: | |
Meta | |
Apple | |
Amazon | |
Netflix | |
Microsoft | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment