001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ArrayUtil.java,v 1.7 2005/01/28 13:50:29 jesper Exp $
023: package net.infonode.util;
024:
025: import java.util.ArrayList;
026:
027: public class ArrayUtil {
028: private ArrayUtil() {
029: }
030:
031: static final public Object[] add(Object[] objects, Object object,
032: Object[] newObjects) {
033: System.arraycopy(objects, 0, newObjects, 0, objects.length);
034: newObjects[objects.length] = object;
035: return newObjects;
036: }
037:
038: static final public byte[] part(byte[] array, int offset, int length) {
039: byte[] b = new byte[length];
040: System.arraycopy(array, offset, b, 0, length);
041: return b;
042: }
043:
044: static final public int countNotNull(Object[] objects) {
045: int count = 0;
046:
047: for (int i = 0; i < objects.length; i++)
048: if (objects[i] != null)
049: count++;
050:
051: return count;
052: }
053:
054: static final public int findSmallest(double[] items) {
055: int index = 0;
056:
057: for (int i = 1; i < items.length; i++) {
058: if (items[i] < items[index]) {
059: index = i;
060: }
061: }
062:
063: return index;
064: }
065:
066: static final public int findSmallest(int[] items) {
067: int index = 0;
068:
069: for (int i = 1; i < items.length; i++) {
070: if (items[i] < items[index]) {
071: index = i;
072: }
073: }
074:
075: return index;
076: }
077:
078: static final public int findLargest(float[] items) {
079: int index = 0;
080:
081: for (int i = 1; i < items.length; i++) {
082: if (items[i] > items[index]) {
083: index = i;
084: }
085: }
086:
087: return index;
088: }
089:
090: public static float[] toFloatArray(int[] values) {
091: float[] floatValues = new float[values.length];
092:
093: for (int i = 0; i < values.length; i++)
094: floatValues[i] = values[i];
095:
096: return floatValues;
097: }
098:
099: static final public int indexOf(int[] array, int value) {
100: for (int i = 0; i < array.length; i++)
101: if (array[i] == value)
102: return i;
103:
104: return -1;
105: }
106:
107: static final public int indexOf(byte[] array, byte value) {
108: for (int i = 0; i < array.length; i++)
109: if (array[i] == value)
110: return i;
111:
112: return -1;
113: }
114:
115: static final public String[] append(String[] a1, String[] a2) {
116: String[] n = new String[a1.length + a2.length];
117: System.arraycopy(a1, 0, n, 0, a1.length);
118: System.arraycopy(a2, 0, n, a1.length, a2.length);
119: return n;
120: }
121:
122: static final public Object[] append(Object[] a1, Object[] a2,
123: Object[] out) {
124: System.arraycopy(a1, 0, out, 0, a1.length);
125: System.arraycopy(a2, 0, out, a1.length, a2.length);
126: return out;
127: }
128:
129: public static boolean equal(int[] a, int aOffset, int[] b,
130: int bOffset, int length) {
131: for (int i = 0; i < length; i++)
132: if (a[aOffset + i] != b[bOffset + i])
133: return false;
134:
135: return true;
136: }
137:
138: public static boolean equal(byte[] a, int aOffset, byte[] b,
139: int bOffset, int length) {
140: for (int i = 0; i < length; i++)
141: if (a[aOffset + i] != b[bOffset + i])
142: return false;
143:
144: return true;
145: }
146:
147: public static boolean contains(short[] a, short v) {
148: for (int i = 0; i < a.length; i++)
149: if (a[i] == v)
150: return true;
151:
152: return false;
153: }
154:
155: public static int[] range(int start, int length, int step) {
156: int[] a = new int[length];
157:
158: for (int i = 0; i < length; i++)
159: a[i] = start + step * i;
160:
161: return a;
162: }
163:
164: public static boolean containsEqual(Object[] values, Object value) {
165: return indexOfEqual(values, value) != -1;
166: }
167:
168: public static boolean contains(Object[] values, Object value) {
169: return indexOf(values, value) != -1;
170: }
171:
172: public static int indexOf(Object[] values, Object value) {
173: for (int i = 0; i < values.length; i++)
174: if (values[i] == value)
175: return i;
176:
177: return -1;
178: }
179:
180: public static int indexOf(Object[] values, Object value,
181: int startIndex, int length) {
182: for (int i = startIndex; i < length; i++)
183: if (values[i] == value)
184: return i;
185:
186: return -1;
187: }
188:
189: public static int indexOfEqual(Object[] values, Object value) {
190: for (int i = 0; i < values.length; i++)
191: if (values[i].equals(value))
192: return i;
193:
194: return -1;
195: }
196:
197: public static Object[] remove(Object[] values, Object value,
198: Object[] newValues) {
199: int index = indexOf(values, value);
200:
201: if (index == -1)
202: index = values.length;
203:
204: System.arraycopy(values, 0, newValues, 0, index);
205: System.arraycopy(values, index + 1, newValues, index,
206: newValues.length - index);
207: return newValues;
208: }
209:
210: public static String toString(int[] a) {
211: StringBuffer b = new StringBuffer(a.length * 4);
212:
213: for (int i = 0; i < a.length; i++) {
214: if (i != 0)
215: b.append(", ");
216:
217: b.append(a[i]);
218: }
219:
220: return b.toString();
221: }
222:
223: public static int[] part(int[] values, int start, int length) {
224: int[] a = new int[length];
225: System.arraycopy(values, start, a, 0, length);
226: return a;
227: }
228:
229: public static int sum(int[] values) {
230: int sum = 0;
231:
232: for (int i = 0; i < values.length; i++)
233: sum += values[i];
234:
235: return sum;
236: }
237:
238: public static int count(int[] values, int value) {
239: int count = 0;
240:
241: for (int i = 0; i < values.length; i++)
242: if (values[i] == value)
243: count++;
244:
245: return count;
246: }
247:
248: public static int count(boolean[] values, boolean value) {
249: int count = 0;
250:
251: for (int i = 0; i < values.length; i++)
252: if (values[i] == value)
253: count++;
254:
255: return count;
256: }
257:
258: public static int findLargest(int[] items) {
259: int index = 0;
260:
261: for (int i = 1; i < items.length; i++) {
262: if (items[i] > items[index]) {
263: index = i;
264: }
265: }
266:
267: return index;
268: }
269:
270: public static int[] toIntArray(ArrayList items) {
271: int[] result = new int[items.size()];
272:
273: for (int i = 0; i < items.size(); i++)
274: result[i] = ((Number) items.get(i)).intValue();
275:
276: return result;
277: }
278:
279: public static boolean[] toBooleanArray(ArrayList items) {
280: boolean[] result = new boolean[items.size()];
281:
282: for (int i = 0; i < items.size(); i++)
283: result[i] = ((Boolean) items.get(i)).booleanValue();
284:
285: return result;
286: }
287: }
|