Converting a Collection of String to an Array : Array Collections « Collections Data Structure « Java






Converting a Collection of String to an Array

Converting a Collection of String to an 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]));
  }
}
           
         
    
    
    
    
  








Related examples in the same category

1.Array Iterator
2.Array MapArray Map
3.Array SetArray Set
4.Array Int Set
5.Remove duplicate element from array
6.Convert an Array to a List
7.Converting an Array to a Collection
8.Converting a Collection of user objects to an Array
9.Create an array containing the elements in a set
10.Convert an array to a Map
11.Treating an Array as an Enumeration
12.ArrayEnumeration class (implements Enumeration)ArrayEnumeration class (implements Enumeration)
13.Custom ArrayMap implementation (extends AbstractMap)Custom ArrayMap implementation (extends AbstractMap)
14.Custom ArraySet implementation (extends AbstractSet)Custom ArraySet implementation (extends AbstractSet)
15.Converts array into a java.util.Map.
16.Growable array of intsGrowable array of ints
17.Growable array of floats.
18.Acts like an java.util.ArrayList but for primitive int valuesActs like an java.util.ArrayList but for primitive int values
19.Acts like an java.util.ArrayList but for primitive long values
20.Add array to collection