⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayutils.java

📁 Bluetooth chat Server and Client in j2me
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	public static void checkIsArray(final Object array,			final boolean allowEnumeration) {		if (!allowEnumeration && (array instanceof Enumeration)) {			// throw the exception		} else {			if (isArray(array))				return;		}		throw new IllegalArgumentException(				"not an array: " + array == null ? "null" : array //$NON-NLS-1$ //$NON-NLS-2$						.getClass().toString());	}	private static final byte[] BYTE = new byte[0];	private static final boolean[] BOOL = new boolean[0];	private static final long[] LONG = new long[0];	private static final int[] INT = new int[0];	private static final Object[] OBJ = new Object[0];	private static final short[] SHORT = new short[0];	/**	 * <p>	 * Returns length of given array.	 * </p>	 * <p>	 * Note: there is no way to get number of items for Enumeration but to walk	 * through it. This effectively 'destroys' given enumeration instance as it	 * is no longer usable.	 * </p>	 * 	 * @param array	 *            the array to check. See {@link #arrayToEnumeration(Object)}	 *            for details.	 * @return length of an array.	 */	public static int getLength(final Object array) {		checkIsArray(array, true);		final Class c = array.getClass();		if (c.isInstance(BYTE))			return ((byte[]) array).length;		else if (c.isInstance(BOOL))			return ((boolean[]) array).length;		else if (c.isInstance(LONG))			return ((long[]) array).length;		else if (c.isInstance(INT))			return ((int[]) array).length;		else if (c.isInstance(OBJ))			return ((Object[]) array).length;		else if (c.isInstance(SHORT))			return ((short[]) array).length;		else if (array instanceof Vector)			return ((Vector) array).size();		else if (array instanceof Queue)			return ((Queue) array).occupiedSlots();		else {			int i = 0;			for (; ((Enumeration) array).hasMoreElements();) {				i++;			}			return i;		}	}	/**	 * Retrieves item from given array.	 * 	 * @param array	 *            the array instance. For details please see	 *            {@link #arrayToEnumeration(Object)}.	 * @param index	 *            the index of array.	 * @return object at given index.	 */	public static Object getElementAt(Object array, int index) {		checkIsArray(array, false);		final Class c = array.getClass();		if (c.isInstance(BYTE))			return new Byte(((byte[]) array)[index]);		else if (c.isInstance(BOOL))			return new Boolean(((boolean[]) array)[index]);		else if (c.isInstance(LONG))			return new Long(((long[]) array)[index]);		else if (c.isInstance(INT))			return new Integer(((int[]) array)[index]);		else if (c.isInstance(OBJ))			return ((Object[]) array)[index];		else if (c.isInstance(SHORT))			return new Short(((short[]) array)[index]);		else if (array instanceof Vector)			return ((Vector) array).elementAt(index);		else			return ((Queue) array).peek(index);	}	/**	 * Sets item into given array.	 * 	 * @param array	 *            the array instance. For details please see	 *            {@link #arrayToEnumeration(Object)}. Note that {@link Queue}	 *            does not support setting of arbitrary item.	 * @param index	 *            the index of array.	 * @param object	 *            object to set.	 */	public static void setElementAt(Object array, int index, final Object object) {		checkIsArray(array, false);		final Class c = array.getClass();		if (c.isInstance(BYTE))			((byte[]) array)[index] = ((Byte) object).byteValue();		else if (c.isInstance(BOOL))			((boolean[]) array)[index] = ((Boolean) object).booleanValue();		else if (c.isInstance(LONG))			((long[]) array)[index] = ((Long) object).longValue();		else if (c.isInstance(INT))			((int[]) array)[index] = ((Integer) object).intValue();		else if (c.isInstance(OBJ))			((Object[]) array)[index] = object;		else if (c.isInstance(SHORT))			((short[]) array)[index] = ((Short) object).shortValue();		else if (array instanceof Vector)			((Vector) array).setElementAt(object, index);		else			throw new IllegalArgumentException("Queue is not supported"); //$NON-NLS-1$	}	/**	 * Converts given object into an enumeration. For details see	 * {@link #arrayToEnumeration(Object)}.	 * 	 * @author Martin Vysny	 */	public static final class ArrayEnumeration implements Enumeration {		/**		 * Constructor.		 * 		 * @param array		 *            array instance.		 */		ArrayEnumeration(final Object array) {			super();			this.array = array;			length = getLength(array);		}		/**		 * The array.		 */		private final Object array;		/**		 * The length of given array.		 */		private final int length;		/**		 * Item to be offered by the enumeration.		 */		private int currentItem = 0;		/*		 * (non-Javadoc)		 * 		 * @see java.util.Enumeration#hasMoreElements()		 */		public boolean hasMoreElements() {			return currentItem < length;		}		/*		 * (non-Javadoc)		 * 		 * @see java.util.Enumeration#nextElement()		 */		public Object nextElement() {			if (!hasMoreElements())				throw new NoSuchElementException();			return getElementAt(array, currentItem++);		}	}	/**	 * Clones given array. For details please see	 * {@link #arrayToEnumeration(Object)}.	 * 	 * @param array	 *            the array to clone. {@link Enumeration}s are not supported.	 * @param deep	 *            if <code>true</code> then sub-arrays are cloned aswell. Note	 *            that if you introduce a nesting cycle the function will	 *            recurse until stack overflow is reached.	 * @return array of same class but cloned. Returns <code>null</code> if	 *         <code>null</code> array was given.	 */	public static Object clone(final Object array, final boolean deep) {		if (array == null)			return null;		if (array instanceof Queue) {			if (!deep)				return new Queue((Queue) array);			final Queue q = (Queue) array;			final Queue result = new Queue(q.bufferSize);			for (final Enumeration e = q.getEnumeration(); e.hasMoreElements();) {				Object obj = e.nextElement();				if (isArray(obj))					obj = clone(obj, true);				result.offer(obj);			}			return result;		}		final Class c = array.getClass();		if (c.isInstance(BYTE)) {			final byte[] a = (byte[]) array;			final byte[] result = new byte[a.length];			for (int i = 0; i < a.length; i++)				result[i] = a[i];			return result;		} else if (c.isInstance(BOOL)) {			final boolean[] a = (boolean[]) array;			final boolean[] result = new boolean[a.length];			for (int i = 0; i < a.length; i++)				result[i] = a[i];			return result;		} else if (c.isInstance(LONG)) {			final long[] a = (long[]) array;			final long[] result = new long[a.length];			for (int i = 0; i < a.length; i++)				result[i] = a[i];			return result;		} else if (c.isInstance(INT)) {			final int[] a = (int[]) array;			final int[] result = new int[a.length];			for (int i = 0; i < a.length; i++)				result[i] = a[i];			return result;		} else if (c.isInstance(OBJ)) {			final Object[] a = (Object[]) array;			final Object[] result = new Object[a.length];			for (int i = 0; i < a.length; i++) {				Object obj = a[i];				if (deep && isArray(obj))					obj = clone(obj, true);				result[i] = obj;			}			return result;		} else if (c.isInstance(SHORT)) {			final short[] a = (short[]) array;			final short[] result = new short[a.length];			for (int i = 0; i < a.length; i++)				result[i] = a[i];			return result;		} else if (array instanceof Vector) {			final Vector a = (Vector) array;			final Vector result = new Vector(a.size());			for (int i = 0; i < a.size(); i++) {				Object obj = a.elementAt(i);				if (deep && isArray(obj))					obj = clone(obj, true);				result.addElement(obj);			}			return result;		} else			throw new IllegalArgumentException("Unsupported array class: " //$NON-NLS-1$					+ c.getName());	}	/**	 * Converts given array to a string representation.	 * 	 * @param array	 *            the array to convert.	 * @return string representation of the array.	 */	public static String arrayToString(final Object array) {		if (array == null)			return "null"; //$NON-NLS-1$		checkIsArray(array, true);		final StringBuffer result = new StringBuffer("["); //$NON-NLS-1$		for (final Enumeration e = arrayToEnumeration(array); e				.hasMoreElements();) {			final Object obj = e.nextElement();			if (isArray(obj)) {				result.append(arrayToString(obj));			} else {				if (obj == null)					result.append("null"); //$NON-NLS-1$				else					result.append(obj);			}			if (e.hasMoreElements())				result.append(", "); //$NON-NLS-1$		}		result.append(']');		return result.toString();	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -