Convert Collection into Array
From Java Example Source Code
Contents |
[edit] Overview - Convert Collection into Array
This Java example program shows how to convert Collection into Array.
[edit] Java Source Code
- Package: example.array
- File: PlanetSet.java
package example.array; import java.util.ArrayList; import java.util.Collection; public class PlanetSet { public static void main(String args[]) { String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" }; Collection planets = new ArrayList(); for (int i = 0, n = names.length; i < n; i++) { planets.add(names[i]); } String s[] = (String[]) planets.toArray(new String[0]); for (int i = 0, n = s.length; i < n; i++) { System.out.println(s[i]); } planets.remove(names[3]); System.out.println(names[1] + " " + planets.contains(names[1])); System.out.println(names[3] + " " + planets.contains(names[3])); } }
[edit] What Result You Can Get
Run the program, you will get:
Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto Venus true Mars false
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
