Java Array Resize resizeArray(final T[] array, final int newSize)

Here you can find the source of resizeArray(final T[] array, final int newSize)

Description

Copies the contents of the given array into an array of the specified size.

License

Open Source License

Parameter

Parameter Description
array the array to be copied ("resized")
newSize the new size

Declaration

public static <T> T[] resizeArray(final T[] array, final int newSize) 

Method Source Code

//package com.java2s;
/*/*w w  w . j a  v a  2s  .c  o  m*/
 *  Copyright (C) 2011-2014 Brian Groenke
 *  All rights reserved.
 * 
 *  This file is part of the 2DX Graphics Library.
 *
 *  This Source Code Form is subject to the terms of the
 *  Mozilla Public License, v. 2.0. If a copy of the MPL 
 *  was not distributed with this file, You can obtain one at 
 *  http://mozilla.org/MPL/2.0/.
 */

import java.util.Arrays;

public class Main {
    /**
     * Copies the contents of the given array into an array of the specified
     * size.
     * 
     * @param array
     *            the array to be copied ("resized")
     * @param newSize
     *            the new size
     * @return
     */
    public static <T> T[] resizeArray(final T[] array, final int newSize) {

        return Arrays.copyOf(array, newSize);
        /*
         * Class<T> type = (Class<T>) array.getClass(); T[] narr = (T[])
         * Array.newInstance(type, newSize); for(int i=0;i<narr.length;i++) {
         * if(i < array.length) narr[i] = array[i]; else narr[i] = null; }
         * return narr;
         */
    }
}

Related

  1. prepend(String firstElement, String[] remaining)
  2. prepend(T element, T[] array)
  3. prependArg(String args[], String newArg)
  4. prependZeros(int n, byte[] message)
  5. resize2(double[][] array, int newsize1, int newsize2, double padding)
  6. resizeNoAutoInit(T[] oldarray, int newsize)