KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > taskblocks > ArrayUtils


1 /*
2  * Copyright (C) Jakub Neubauer, 2007
3  *
4  * This file is part of TaskBlocks
5  *
6  * TaskBlocks is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * TaskBlocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */

19
20 package taskblocks;
21
22
23 import java.lang.reflect.Array JavaDoc;
24
25 /**
26  * Set of utilities used to manipulate arrays.
27  *
28  * @author j.neubauer
29  * @since 11.1.2007
30  */

31 public class ArrayUtils {
32     
33     /**
34      * Creates new array of the same type as given array and adds specified member
35      * to the end of new array.
36      *
37      * @param array
38      * @param member
39      * @return new array with member appened
40      */

41     public static Object JavaDoc[] addToArray(Object JavaDoc[] array, Object JavaDoc member) {
42         Object JavaDoc[] newArray = (Object JavaDoc[])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     /**
49      * Finds member in array and if finds it, creates the same array without this member.
50      *
51      * @param array
52      * @param member
53      * @return new array without member or the old array if member wasn't found
54      */

55     public static Object JavaDoc[] removeFromArray(Object JavaDoc[] array, Object JavaDoc member) {
56         int i;
57         for(i = 0; i<array.length;i++) {
58             if(array[i] == member) {
59                 break;
60             }
61         }
62         // if we found the listener, construct new arrray without it.
63
if(i < array.length) {
64             Object JavaDoc[] newArray = (Object JavaDoc[])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 JavaDoc[] removeFromArray(Object JavaDoc[] array, int i) {
73         if(i < 0 || i >= array.length) {
74             throw new IndexOutOfBoundsException JavaDoc();
75         }
76         Object JavaDoc[] newArray = (Object JavaDoc[])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