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

📄 arrayutils.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

	public static int[] delete(int[] arr, int idx) {
		int newlen = arr.length - 1;
		if (newlen < 0) throw new IllegalArgumentException("cannot delete from an empty array");
		int[] res = new int[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;
	}

	public static double[] delete(double[] arr, int idx) {
		int newlen = arr.length - 1;
		if (newlen < 0) throw new IllegalArgumentException("cannot delete from an empty array");
		double[] res = new double[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;
	}

	public static double[][] delete2_1(double[][] arr, int idx) {
		int newlen = arr.length - 1;
		if (newlen < 0) throw new IllegalArgumentException("cannot delete from an empty array");
		double[][] res = new double[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;
	}

	public static double[][] delete2_2(double[][] arr, int idx) {
		for (int i = 0; i < arr.length; i++) {
			arr[i] = delete(arr[i], idx);
		}
		return arr;
	}

	public static double[][][] delete3_1(double[][][] arr, int idx) {
		int newlen = arr.length - 1;
		if (newlen < 0) throw new IllegalArgumentException("cannot delete from an empty array");
		double[][][] res = new double[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;
	}

	public static double[][][] delete3_2(double[][][] arr, int idx) {
		for (int i = 0; i < arr.length; i++) {
			arr[i] = delete2_1(arr[i], idx);
		}
		return arr;
	}

	public static double[][][] delete3_3(double[][][] arr, int idx) {
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				arr[i][j] = delete(arr[i][j], idx);
			}
		}
		return arr;
	}

	/** for each i,j result[i][j] = array[i][index2][j-1] */
	public static double[][] extract13(double[][][] array, int index2) {
		int len1 = array.length;
		double[][] res = new double[len1][];

		for (int i = 0; i < len1; i++) {
			res[i] = array[i][index2];
		}

		return res;
	}

	/** for each i result[i] = array[i][index2] */
	public static double[] extract1(double[][] array, int index2) {
		int len = array.length;
		double[] res = new double[len];

		for (int i = 0; i < len; i++) {
			res[i] = array[i][index2];
		}
		return res;
	}

	/** create an empty double[len1][len2] */
	public static double[][] makeFilled(int len1, int len2, double val) {
		boolean fill = (val != 0);
		double[][] res = new double[len1][];
		for (int i = 0; i < len1; i++) {
			res[i] = new double[len2];
			if (fill) {
				Arrays.fill(res[i], val);
			}
		}
		return res;
	}

	/** for each i result[i][index2] = array[i] */
	public static void insert1(double[][] result, double[] array, int index2) {
		for (int i = 0; i < array.length; i++) {
			result[i][index2] = array[i];
		}
	}

	/** for each i result[i]=(int)array[i] */
	public static int[] toInt(double[] arr) {
		int len = arr.length;
		int[] res = new int[len];
		for (int i = 0; i < len; i++) {
			res[i] = (int) arr[i];
		}
		return res;
	}

	public static String toCSV(double[] array) {
		if (array.length == 0) return "";
		StringBuffer s = new StringBuffer();

		s.append(Double.toString(array[0]));

		for (int i = 1; i < array.length; i++) {
			s.append(";").append(Double.toString(array[i]));
		}
		return s.toString();
	}

	public static String toCSV3_2(double[][][] array, int index1, int index3) {
		int len2 = array[index1].length;
		if (len2 == 0) return "";
		StringBuffer s = new StringBuffer();

		s.append(Double.toString(array[index1][0][index3]));

		for (int i = 1; i < len2; i++) {
			s.append(";").append(Double.toString(array[index1][i][index3]));
		}
		return s.toString();
	}

	public static boolean fromCSV(double[] array, String str) {
		String[] tokens = str.split(";");
		int ntok = tokens.length;
		if (ntok != array.length) return false;
		for (int i = 0; i < ntok; i++) {
			array[i] = Double.parseDouble(tokens[i]);
		}
		return true;
	}

    /**
     * Extract a double[] given a string separated by ';'
     * @param str input string
     * @return double array
     * <br>
     * Author: Bertoli Marco
     */
    public static double[] fromCSV(String str) {
        String[] tokens = str.split(";");
        double[] out = new double[tokens.length];
        for (int i = 0; i < out.length; i++) {
            out[i] = Double.parseDouble(tokens[i]);
        }
        return out;
    }

    public static boolean fromCSV3_2(double[][][] array, int index1, int index3, String str) {
		String[] tokens = str.split(";");
		int ntok = tokens.length;
		if (ntok != array[index1].length) return false;

		for (int i = 0; i < ntok; i++) {
			array[index1][i][index3] = Double.parseDouble(tokens[i]);
		}
		return true;
	}

	/* good ol' Arrays only has 'em for 1 dimension...*/
	public static boolean equals2(double[][] arr1, double[][] arr2) {
		int len1 = arr1.length;
		if (len1 != arr2.length) return false;
		for (int i = 0; i < len1; i++) {
			if (!Arrays.equals(arr1[i], arr2[i])) return false;
		}
		return true;
	}

	public static boolean equals3(double[][][] arr1, double[][][] arr2) {
		int len1 = arr1.length;
		if (len1 != arr2.length) return false;
		for (int i = 0; i < len1; i++) {
			if (!equals2(arr1[i], arr2[i])) return false;
		}
		return true;
	}

	public static String toString(Object[] array) {
		StringBuffer s = new StringBuffer("[ ");
		for (int i = 0; i < array.length; i++) {
			if (array[i] == null) {
				s.append("null");
			} else {
				s.append(array[i].toString());
			}
			s.append(" ");
		}
		s.append("]");

		return s.toString();
	}

	public static String toString(int[] array) {
		StringBuffer s = new StringBuffer("[ ");
		for (int i = 0; i < array.length; i++) {
			s.append(array[i]).append(" ");
		}
		s.append("]");

		return s.toString();
	}

	public static String toString(double[] array) {
		StringBuffer s = new StringBuffer("[ ");
		for (int i = 0; i < array.length; i++) {
			s.append(array[i]).append(" ");
		}
		s.append("]");

		return s.toString();
	}

	public static String toString2(double[][] array) {
		StringBuffer s = new StringBuffer("[ ");
		for (int i = 0; i < array.length; i++) {
			s.append(toString(array[i])).append(" ");
		}
		s.append("]");

		return s.toString();
	}

	public static String toString3(double[][][] array) {
		StringBuffer s = new StringBuffer("[ ");
		for (int i = 0; i < array.length; i++) {
			s.append(toString2(array[i])).append(" ");
		}
		s.append("]");

		return s.toString();
	}


	/**
	 * array[n]->result[n+1], where
	 *
	 * result[0]=0
	 * and
	 * for i=(0..n-1) result[i]=array[i-1]
	 *
	 */
	public static double[] prepend0(double[] array) {
		int len = array.length;
		double[] res = new double[len + 1];
		System.arraycopy(array, 0, res, 1, len);
		return res;
	}

    /**
     * Copies a 2D array in a 3D one. Given source[i][j] elements are copied in
     * position dest[i][j][k] where k is a given index.
     * <br>
     * Author: Bertoli Marco
     * @param source source 2D array
     * @param dest destination 3D array
     * @param k third coordinate to be left unchanged
     */
    public static void copy2to3(double[][] source, double[][][] dest, int k) {
        for (int i=0; i<source.length; i++)
            for (int j=0; j<source[i].length; j++)
                dest[i][j][k] = source[i][j];
    }

    /**
     * Given an array <em>A</em> and a number <em>n</em> returns the product <em>A*n</em>
     * <br>
     * Author: Bertoli Marco
     * @param source source array <em>A</em>
     * @param num number to be multiplied <em>n</em>
     * @return a new array with multiplied value
     */
    public static double[] multiply(double[] source, double num) {
        double[] ret = new double[source.length];
        for (int i=0; i<ret.length; i++)
            ret[i] = source[i] * num;
        return ret;
    }
}

⌨️ 快捷键说明

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