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

📄 arrayutils.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**    
  * 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
  */
  
package jmt.gui.exact.utils;

import java.util.Arrays;

/**

 * @author alyf (Andrea Conti)
 * Date: 11-set-2003
 * Time: 16.46.05

 Modified by Bertoli Marco 18-may-2006

 */

/**
 * Assorted array mangling stuff.<br>
 * <li><code>resize</code> methods return a resized copy of an array, preserving data where possible and padding with a specified value
 * <li><code>copy</code> methods perform a deep copy of an array. They come in two flavors: one that copies into an existing array (that must have been properly initialized) and one that creates the copy from scratch
 * <li><code>delete</code> methods delete an element in an array, returning a reduced-size copy in which following elements have been shifted to fill the gap. For multidim arrays shallow copy is performed when possible - ie do not assume the new array to be independent from the old one
 * <li><code>toCSV</code> and <code>fromCSV</code> methods input/output single rows as Strings of comma-separated values.
 * <li><code>toString</code> methods return a somewhat useful String representation of an array.
 * <p><b><i>NOTE:</i></b> <code>fromCSV</code> uses the new regex String splitting methods introduced in 1.4. For use in pre-1.4 JDKs it must be rewritten using StringTokenizer.
 *
 */
public class ArrayUtils {

	private ArrayUtils() {
	}

	/**
	 * 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;
	}

	/**
	 * 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 a 3-dimensional array with variable sizes preserving data and padding as needed
	 */
	public static double[][][] resize3var(double[][][] array, int newsize1, int newsize2, int[] newsize3, double padding) {
		if (newsize1 < 0 || newsize2 < 0) throw new RuntimeException("newsizes must be >=0");

		int oldsize1 = array.length;

		//1st dimension
		double[][][] newArray = new double[newsize1][][];

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

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

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

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

		return newArray;
	}

	public static String[] copy(String[] array) {
		String[] newArray = new String[array.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	public static double[] copy(double[] array) {
		double[] newArray = new double[array.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	public static int[] copy(int[] array) {
		int[] newArray = new int[array.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	/* all multidim copy methods written explicitly to avoid some call overhead.
	Multidim copiers correctly handle non-square arrays */

	/**
	 * @return a deep copy of <code>array</code>
	 */
	public static double[][] copy2(double[][] array) {
		int len1,len2;
		len1 = array.length;
		double[][] newArray = new double[len1][];
		System.arraycopy(array, 0, newArray, 0, len1);
		for (int i = 0; i < len1; i++) {
			len2 = array[i].length;
			newArray[i] = new double[len2];
			System.arraycopy(array[i], 0, newArray[i], 0, len2);
		}

		return newArray;
	}

	/**
	 * @return a deep copy of <code>array</code>
	 */
	public static double[][][] copy3(double[][][] array) {

		int len1,len2,len3;
		len1 = array.length;
		double[][][] newArray = new double[len1][][];
		System.arraycopy(array, 0, newArray, 0, len1);
		for (int i = 0; i < len1; i++) {
			len2 = array[i].length;
			newArray[i] = new double[len2][];
			System.arraycopy(array[i], 0, newArray[i], 0, len2);
			for (int j = 0; j < len2; j++) {
				len3 = array[i][j].length;
				newArray[i][j] = new double[len3];
				System.arraycopy(array[i][j], 0, newArray[i][j], 0, len3);
			}
		}

		return newArray;
	}

    public static double[][][] copy3per2(double[][][] array3, double[][] factor){
        double[][][] newArray = new double[array3.length][][];
        for (int i = 0; i < array3.length; i++) {
            newArray[i] = new double[array3[i].length][];
            if(i<factor.length){
                for (int j = 0; j < array3[i].length; j++) {
                    newArray[i][j] = new double[array3[i][j].length];
                    if(j<factor[i].length){
                        for(int k=0; k<array3[i][j].length; k++){
                                newArray[i][j][k] = array3[i][j][k]*factor[i][j];
                        }
                    }else return copy3(array3);
                }
            }else return copy3(array3);
        }
        return newArray;
    }

	public static void copy(String[] from, String[] to) {
		System.arraycopy(from, 0, to, 0, from.length);
	}

	public static void copy(double[] from, double[] to) {
		System.arraycopy(from, 0, to, 0, from.length);
	}

	public static void copy(int[] from, int[] to) {
		System.arraycopy(from, 0, to, 0, from.length);
	}

	public static void copy2(double[][] from, double[][] to) {
		int len1,len2;
		len1 = from.length;
		System.arraycopy(from, 0, to, 0, len1);
		for (int i = 0; i < len1; i++) {
			len2 = from[i].length;
			System.arraycopy(from[i], 0, to[i], 0, len2);
		}
	}

	public static void copy3(double[][][] from, double[][][] to) {

		int len1,len2,len3;
		len1 = from.length;
		System.arraycopy(from, 0, to, 0, len1);
		for (int i = 0; i < len1; i++) {
			len2 = from[i].length;
			System.arraycopy(from[i], 0, to[i], 0, len2);
			for (int j = 0; j < len2; j++) {
				len3 = from[i][j].length;
				System.arraycopy(from[i][j], 0, to[i][j], 0, len3);
			}
		}
	}

	public static String[] delete(String[] arr, int idx) {
		int newlen = arr.length - 1;
		if (newlen < 0) throw new IllegalArgumentException("cannot delete from an empty array");
		String[] res = new String[newlen];
		if (idx > 0) {
			System.arraycopy(arr, 0, res, 0, idx);
		}
		if (idx < newlen) {
			System.arraycopy(arr, idx + 1, res, idx, newlen - idx);
		}
		return res;
	}

⌨️ 快捷键说明

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