1 package com.ca.commons.cbutil; 2 3 import java.util.Enumeration ; 4 import java.util.Vector ; 5 6 7 14 15 16 public class CBArray 17 { 18 19 30 31 public static Object [] union(Object [] a, Object [] b) 32 { 33 Object [] temp = new Object [a.length + b.length]; 35 int i = 0; 36 for (i = 0; i < a.length; i++) temp[i] = a[i]; 37 for (int j = 0; j < b.length; j++) 38 if (contains(a, b[j]) == false) temp[i++] = b[j]; 40 41 44 if (a.length + b.length == i) 45 return temp; 46 else 47 { 48 Object [] ret = new Object [i]; 49 for (int k = 0; k < i; k++) ret[k] = temp[k]; 50 51 return ret; 52 } 53 } 54 55 65 66 public static Object [] difference(Object [] a, Object [] b) 67 { 68 if (a == null || b == null) return null; 70 if (a.length == 0) return ((Object []) a.clone()); 71 if (b.length == 0) return ((Object []) a.clone()); 72 73 Object [] temp = new Object [a.length]; 74 int len = 0; 75 for (int i = 0; i < a.length; i++) 76 if (contains(b, a[i]) == false) 77 temp[len++] = a[i]; 78 79 Object ret[] = new Object [len]; 80 for (int i = 0; i < len; i++) ret[i] = temp[i]; 81 82 85 return ret; 86 } 87 88 99 100 public static Object [] intersection(Object [] a, Object [] b) 101 { 102 if (a == null || b == null) return null; 104 if (a.length == 0) return ((Object []) a.clone()); 105 if (b.length == 0) return ((Object []) b.clone()); 106 107 if (a.length > b.length) { 109 Object [] c = a; 110 a = b; 111 b = c; 112 } 113 114 Object [] temp = new Object [a.length]; 115 116 int len = 0; 117 for (int i = 0; i < a.length; i++) 118 if (contains(b, a[i]) == true) temp[len++] = a[i]; 119 120 Object [] ret = new Object [len]; 121 for (int i = 0; i < len; i++) ret[i] = temp[i]; 122 return ret; 123 } 124 125 129 130 public static boolean isOrderedEqual(Object [] a, Object [] b) 131 { 132 if (a == null && b == null) return true; 133 if (a == null || b == null) return false; 134 if (a.length != b.length) return false; 135 for (int i = 0; i < a.length; i++) 136 if (a[i].equals(b[i]) == false) return false; 137 return true; 138 } 139 140 145 public static boolean isUnorderedEqual(Object [] a, Object [] b) 146 { 147 if (a == null && b == null) return true; 148 if (a == null || b == null) return false; 149 if (a.length != b.length) return false; 150 for (int i = 0; i < a.length; i++) 151 if (contains(b, a[i]) == false) return false; 152 return true; 153 } 154 155 162 public static boolean contains(Object array[], Object o) 163 { 164 if (o == null) return false; 165 for (int i = 0; i < array.length; i++) 166 if (o.equals(array[i])) return true; 167 return false; 168 } 169 170 173 174 public static void print(Object array[]) 175 { 176 for (int i = 0; i < array.length; i++) 177 System.out.println(i + ": " + array[i].toString()); 178 } 179 180 181 public static Object [] enumerationToArray(Enumeration e) 182 { 183 Vector v = new Vector (); 184 while (e.hasMoreElements()) v.add(e.nextElement()); 185 return v.toArray(); 186 } 187 188 public static Enumeration arrayToEnumeration(Object [] o) 189 { 190 Vector v = new Vector (o.length); 191 for (int i = 0; i < o.length; i++) v.add(o[i]); 192 return v.elements(); 193 } 194 195 public static Object [] trimNulls(Object [] o) 196 { 197 Vector v = new Vector (o.length); 198 for (int i = 0; i < o.length; i++) 199 if (o[i] != null) 200 v.add(o[i]); 201 return v.toArray(); 202 } 203 } | Popular Tags |