Java tutorial
//package com.java2s; import java.lang.reflect.*; public class Main { /** * Expands an array of Objects to double of its current size. Remember to use cast to turn the result into an array of the appropriate class, i.e: * * <code>someArray=(SomeClass [])unlekker.util.Util.expandArray(someArray); * @param list Array to be expanded. * @return Array of Object [], must be cast to appropriate class. */ static public Object expandArray(Object[] list) { int newSize = list.length * 2; Class type = list.getClass().getComponentType(); Object temp = Array.newInstance(type, newSize); System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize)); return temp; } static public Object expandArray(Object[] list, int newSize) { Class type = list.getClass().getComponentType(); Object temp = Array.newInstance(type, newSize); System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize)); return temp; } /** * Expands an array of floats to double its current size. * * @param f Array to be expanded. * @return Array of floats with f.length*2 length. */ static public float[] expandArray(float[] f) { int newSize = f.length * 2; float[] newf = new float[newSize]; System.arraycopy(f, 0, newf, 0, Math.min(f.length, newSize)); return newf; } /** * Expands the first column of a 2D array of floats to * double its current size. * * @param f Array to be expanded. * @return Array of floats with f.length*2 length. */ static public float[][] expandArray(float[][] f) { int newSize = f.length * 2; float[][] newf = new float[newSize][]; for (int i = 0; i < f.length; i++) newf[i] = f[i]; return newf; } static public int[] expandArray(int[] f) { int newSize = f.length * 2; int[] newi = new int[newSize]; System.arraycopy(f, 0, newi, 0, Math.min(f.length, newSize)); return newi; } static public int[] expandArray(int[] f, int newSize) { int[] newi = new int[newSize]; System.arraycopy(f, 0, newi, 0, Math.min(f.length, newSize)); return newi; } public static float min(float[] f) { float min = Float.MAX_VALUE; for (float ff : f) if (ff < min) min = ff; return min; } }