Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Insert value to array

/*  * dbXML - Native XML Database  * Copyright (C) 1999-2004  The dbXML Group, L.L.C.  *  * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  *  * $Id: Array.java,v 1.2 2004/02/12 00:17:58 bradford Exp $  */ /**  * Array utility methods  */ public final class Array {    private Array() {    }    public static Object[] insertArrayObject(Object[] vals, Object val, int idx) {       Object[] newVals = new Object[vals.length + 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       newVals[idx] = val;       if ( idx < vals.length )          System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx);       return newVals;    }    public static Object[] deleteArrayObject(Object[] vals, int idx) {       Object[] newVals = new Object[vals.length - 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       if ( idx < newVals.length )          System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx);       return newVals;    }    public static long[] insertArrayLong(long[] vals, long val, int idx) {       long[] newVals = new long[vals.length + 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       newVals[idx] = val;       if ( idx < vals.length )          System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx);       return newVals;    }    public static long[] deleteArrayLong(long[] vals, int idx) {       long[] newVals = new long[vals.length - 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       if ( idx < newVals.length )          System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx);       return newVals;    }    public static int[] insertArrayInt(int[] vals, int val, int idx) {       int[] newVals = new int[vals.length + 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       newVals[idx] = val;       if ( idx < vals.length )          System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx);       return newVals;    }    public static int[] deleteArrayInt(int[] vals, int idx) {       int[] newVals = new int[vals.length - 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       if ( idx < newVals.length )          System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx);       return newVals;    }    public static short[] insertArrayShort(short[] vals, short val, int idx) {       short[] newVals = new short[vals.length + 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       newVals[idx] = val;       if ( idx < vals.length )          System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx);       return newVals;    }    public static short[] deleteArrayShort(short[] vals, int idx) {       short[] newVals = new short[vals.length - 1];       if ( idx > 0 )          System.arraycopy(vals, 0, newVals, 0, idx);       if ( idx < newVals.length )          System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx);       return newVals;    } }