1 19 20 package taskblocks; 21 22 23 import java.lang.reflect.Array ; 24 25 31 public class ArrayUtils { 32 33 41 public static Object [] addToArray(Object [] array, Object member) { 42 Object [] newArray = (Object [])Array.newInstance(array.getClass().getComponentType(), array.length + 1); 43 System.arraycopy(array, 0, newArray, 0, array.length); 44 newArray[array.length] = member; 45 return newArray; 46 } 47 48 55 public static Object [] removeFromArray(Object [] array, Object member) { 56 int i; 57 for(i = 0; i<array.length;i++) { 58 if(array[i] == member) { 59 break; 60 } 61 } 62 if(i < array.length) { 64 Object [] newArray = (Object [])Array.newInstance(array.getClass().getComponentType(), array.length - 1); 65 System.arraycopy(array, 0, newArray, 0, i); 66 System.arraycopy(array, i+1, newArray, i, array.length-i-1); 67 return newArray; 68 } 69 return array; 70 } 71 72 public static Object [] removeFromArray(Object [] array, int i) { 73 if(i < 0 || i >= array.length) { 74 throw new IndexOutOfBoundsException (); 75 } 76 Object [] newArray = (Object [])Array.newInstance(array.getClass().getComponentType(), array.length - 1); 77 System.arraycopy(array, 0, newArray, 0, i); 78 System.arraycopy(array, i+1, newArray, i, array.length-i-1); 79 return newArray; 80 } 81 } 82 | Popular Tags |