1 23 24 package com.sun.enterprise.tools.common.util; 25 26 import java.util.AbstractCollection ; 27 import java.util.ArrayList ; 28 29 public class ContainerHelper 30 { 31 33 public static String [] toStringArray(AbstractCollection coll) throws ArrayStoreException 34 { 35 String ss[] = new String [0]; 36 37 if(coll.size() > 0) 38 ss = (String []) coll.toArray(ss); 39 40 return ss; 41 } 42 43 45 public static ArrayList toArrayList(String [] ss) 46 { 47 if(ss == null) 48 return new ArrayList (); 49 50 ArrayList list = new ArrayList (ss.length); 51 52 for(int i = 0; i < ss.length; i++) 53 list.add(ss[i]); 54 55 return list; 56 } 57 58 60 public static String toOneString(String [] ss) 61 { 62 String s = new String (); 63 64 for(int i = 0; ss != null && i < ss.length; i++) 65 { 66 s += ss[i] + "\n"; } 68 return s; 69 } 70 71 73 public static void main(String [] args) 74 { 75 ArrayList L1 = new ArrayList (); 76 ArrayList L2 = new ArrayList (); 77 78 L1.add("Hello"); L1.add("World"); L1.add("!!!"); L2.add(new Integer (5)); 82 L2.add(new Integer (7)); 83 84 String ss[] = toStringArray(L1); 85 86 for(int i = 0; i < ss.length; i++) 87 System.out.println("String #" + i + ": " + ss[i]); 89 try 91 { 92 ss = toStringArray(L2); 93 } 94 catch(ArrayStoreException e) 95 { 96 System.out.println("Caught an Exception, as expected: " + e); } 98 } 99 } 100 101 | Popular Tags |