Java tutorial
//package com.java2s; /* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept. This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit). http://www.cs.umass.edu/~mccallum/mallet This software is provided under the terms of the Common Public License, version 1.0, as published by http://www.opensource.org. For further information, see the file `LICENSE' included with this distribution. */ import java.lang.reflect.Array; public class Main { public static String toString(int[] v) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < v.length; i++) { buf.append(v[i]); if (i < v.length - 1) buf.append(" "); } return buf.toString(); } public static String toString(double[] v) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < v.length; i++) { buf.append(v[i]); if (i < v.length - 1) buf.append(" "); } return buf.toString(); } /** * Returns a new array with a single element appended at the end. * Use this sparingly, for it will allocate a new array. You can * easily turn a linear-time algorithm to quadratic this way. * @param v Original array * @param elem Element to add to end */ public static int[] append(int[] v, int elem) { int[] ret = new int[v.length + 1]; System.arraycopy(v, 0, ret, 0, v.length); ret[v.length] = elem; return ret; } /** * Returns a new array with a single element appended at the end. * Use this sparingly, for it will allocate a new array. You can * easily turn a linear-time algorithm to quadratic this way. * @param v Original array * @param elem Element to add to end */ public static boolean[] append(boolean[] v, boolean elem) { boolean[] ret = new boolean[v.length + 1]; System.arraycopy(v, 0, ret, 0, v.length); ret[v.length] = elem; return ret; } /** * Returns a new array with a single element appended at the end. * Use this sparingly, for it will allocate a new array. You can * easily turn a linear-time algorithm to quadratic this way. * @param v Original array * @param elem Element to add to end * @return Array with length v+1 that is (v0,v1,...,vn,elem). * Runtime type will be same as he pased-in array. */ public static Object[] append(Object[] v, Object elem) { Object[] ret = (Object[]) Array.newInstance(v.getClass().getComponentType(), v.length + 1); System.arraycopy(v, 0, ret, 0, v.length); ret[v.length] = elem; return ret; } }