Java Array Resize resize2(double[][] array, int newsize1, int newsize2, double padding)

Here you can find the source of resize2(double[][] array, int newsize1, int newsize2, double padding)

Description

Resizes a 2-dimensional array preserving data and padding as needed

License

Open Source License

Declaration

public static double[][] resize2(double[][] array, int newsize1, int newsize2, double padding) 

Method Source Code

//package com.java2s;
/**    /* w  w  w .  j  a  v  a 2 s. c o m*/
  * Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
    
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
    
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
    
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */

import java.util.Arrays;

public class Main {
    /**
     * Resizes a 2-dimensional array preserving data and padding as needed
     */
    public static double[][] resize2(double[][] array, int newsize1, int newsize2, double padding) {
        if (newsize1 < 0 || newsize2 < 0) {
            throw new RuntimeException("newsizes must be >=0");
        }

        int oldsize1 = array.length;
        int oldsize2 = array[0].length;

        if (oldsize1 == newsize1 && oldsize2 == newsize2) {
            return array;
        }

        //1st dimension

        double[][] newArray = new double[newsize1][];

        int count = (oldsize1 < newsize1 ? oldsize1 : newsize1);
        System.arraycopy(array, 0, newArray, 0, count);

        //2nd dimension
        for (int i = 0; i < count; i++) {
            newArray[i] = resize(array[i], newsize2, padding);
        }

        // fill in nulls
        for (int i = count; i < newsize1; i++) {
            newArray[i] = new double[newsize2];
        }

        //pad
        if (padding != 0.0) {
            for (int i = count; i < newsize1; i++) {
                Arrays.fill(newArray[i], padding);
            }
        }

        return newArray;
    }

    /**
     * Resizes an array, preserving data and padding as needed
     */
    public static int[] resize(int[] array, int newsize, int padding) {
        if (newsize < 0) {
            throw new RuntimeException("newsize must be >=0");
        }
        int oldsize = array.length;
        if (newsize == oldsize) {
            return array;
        }

        int[] newArray = new int[newsize];

        int count = (oldsize < newsize ? oldsize : newsize);

        System.arraycopy(array, 0, newArray, 0, count);

        if (padding != 0) {
            for (int i = count; i < newsize; i++) {
                newArray[i] = padding;
            }
        }

        return newArray;
    }

    /**
     * Resizes an array, preserving data and padding as needed
     */
    public static double[] resize(double[] array, int newsize, double padding) {
        if (newsize < 0) {
            throw new RuntimeException("newsize must be >=0");
        }
        int oldsize = array.length;
        if (newsize == oldsize) {
            return array;
        }
        double[] newArray = new double[newsize];
        int count = (oldsize < newsize ? oldsize : newsize);
        System.arraycopy(array, 0, newArray, 0, count);

        if (padding != 0.0) {
            for (int i = count; i < newsize; i++) {
                newArray[i] = padding;
            }
        }

        return newArray;
    }

    /**
     * Resizes an array, preserving data and padding as needed
     */
    public static String[] resize(String[] array, int newsize, String padding) {
        if (newsize < 0) {
            throw new RuntimeException("newsize must be >=0");
        }
        int oldsize = array.length;
        if (newsize == oldsize) {
            return array;
        }

        String[] newArray = new String[newsize];

        int count = (oldsize < newsize ? oldsize : newsize);
        System.arraycopy(array, 0, newArray, 0, count);

        if (padding != null) {
            for (int i = count; i < newsize; i++) {
                newArray[i] = padding;
            }
        }

        return newArray;
    }
}

Related

  1. arrayResize(double[] source, int targetSize)
  2. prepend(String firstElement, String[] remaining)
  3. prepend(T element, T[] array)
  4. prependArg(String args[], String newArg)
  5. prependZeros(int n, byte[] message)
  6. resizeArray(final T[] array, final int newSize)
  7. resizeNoAutoInit(T[] oldarray, int newsize)