Created
December 24, 2021 08:20
-
-
Save gauravkukade/323b923fe1441207fc2ae3cb35b86176 to your computer and use it in GitHub Desktop.
A java program to convert an ArrayList to array using toArray() method. toArray() method returns Object[], 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
/** | |
* A java program to convert an ArrayList to array using | |
* toArray method. | |
* | |
* toArray() method returns Object[] | |
* | |
* @author Gaurav Kukade at coderolls.com | |
* | |
*/ | |
public class ArrayListToArray { | |
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 | |
Object[] objects = arrayList.toArray(); | |
System.out.println("\nArray:"); | |
//we will receive the object array, so iterate on it to print each element | |
for(Object obj:objects) { | |
System.out.println(obj); | |
} | |
} | |
} |
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