Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * An empty immutable {@code Long} array. */ public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0]; /** * An empty immutable {@code Integer} array. */ public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0]; /** * An empty immutable {@code Short} array. */ public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0]; /** * An empty immutable {@code Byte} array. */ public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0]; /** * An empty immutable {@code Double} array. */ public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0]; /** * An empty immutable {@code Float} array. */ public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0]; /** * An empty immutable {@code Boolean} array. */ public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0]; /** * An empty immutable {@code Character} array. */ public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0]; /** * <p>Converts an array of primitive chars to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code char} array * @return a {@code Character} array, {@code null} if null array input */ public static Character[] toObject(char[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_CHARACTER_OBJECT_ARRAY; } final Character[] result = new Character[array.length]; for (int i = 0; i < array.length; i++) { result[i] = Character.valueOf(array[i]); } return result; } /** * <p>Converts an array of primitive longs to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code long} array * @return a {@code Long} array, {@code null} if null array input */ public static Long[] toObject(long[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_LONG_OBJECT_ARRAY; } final Long[] result = new Long[array.length]; for (int i = 0; i < array.length; i++) { result[i] = Long.valueOf(array[i]); } return result; } /** * <p>Converts an array of primitive ints to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array an {@code int} array * @return an {@code Integer} array, {@code null} if null array input */ public static Integer[] toObject(int[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_INTEGER_OBJECT_ARRAY; } final Integer[] result = new Integer[array.length]; for (int i = 0; i < array.length; i++) { result[i] = Integer.valueOf(array[i]); } return result; } /** * <p>Converts an array of primitive shorts to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code short} array * @return a {@code Short} array, {@code null} if null array input */ public static Short[] toObject(short[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_SHORT_OBJECT_ARRAY; } final Short[] result = new Short[array.length]; for (int i = 0; i < array.length; i++) { result[i] = Short.valueOf(array[i]); } return result; } /** * <p>Converts an array of primitive bytes to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code byte} array * @return a {@code Byte} array, {@code null} if null array input */ public static Byte[] toObject(byte[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_BYTE_OBJECT_ARRAY; } final Byte[] result = new Byte[array.length]; for (int i = 0; i < array.length; i++) { result[i] = Byte.valueOf(array[i]); } return result; } /** * <p>Converts an array of primitive doubles to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code double} array * @return a {@code Double} array, {@code null} if null array input */ public static Double[] toObject(double[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_DOUBLE_OBJECT_ARRAY; } final Double[] result = new Double[array.length]; for (int i = 0; i < array.length; i++) { result[i] = Double.valueOf(array[i]); } return result; } /** * <p>Converts an array of primitive floats to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code float} array * @return a {@code Float} array, {@code null} if null array input */ public static Float[] toObject(float[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_FLOAT_OBJECT_ARRAY; } final Float[] result = new Float[array.length]; for (int i = 0; i < array.length; i++) { result[i] = Float.valueOf(array[i]); } return result; } /** * <p>Converts an array of primitive booleans to objects.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array a {@code boolean} array * @return a {@code Boolean} array, {@code null} if null array input */ public static Boolean[] toObject(boolean[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_BOOLEAN_OBJECT_ARRAY; } final Boolean[] result = new Boolean[array.length]; for (int i = 0; i < array.length; i++) { result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE); } return result; } }