Returns a copy of the specified array of objects of the specified size. : Array Insert Remove « Collections Data Structure « Java






Returns a copy of the specified array of objects of the specified size.

      
/*
 * LingPipe v. 3.9
 * Copyright (C) 2003-2010 Alias-i
 *
 * This program is licensed under the Alias-i Royalty Free License
 * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Alias-i
 * Royalty Free License Version 1 for more details.
 *
 * You should have received a copy of the Alias-i Royalty Free License
 * Version 1 along with this program; if not, visit
 * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact
 * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211,
 * +1 (718) 290-9170.
 */

//package com.aliasi.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Static utility methods for processing arrays.
 *
 * @author  Bob Carpenter
 * @version 4.0.0
 * @since   LingPipe1.0
 */
public class Arrays {
    /**
     * Returns a copy of the specified array of objects of the
     * specified size.  The runtime type of the array returned will be
     * that of the specified input array.
     *
     * <p>As many of the original elements as will fit
     * in the new array are copied into the returned array.  If the
     * new size is longer, the remaining elements will be null.
     *
     * @param xs Original array.
     * @param newSize Size of returned array.
     * @return New array of specified length with as many elements copied
     * from the original array as will fit.
     * @param <E> type of objects in array.
     */
    public static <E> E[] reallocate(E[] xs, int newSize) {
        @SuppressWarnings("unchecked") // must work because of type system and reflect.Array
        E[] ys 
            = (E[])
            java.lang.reflect.Array
            .newInstance(xs.getClass().getComponentType(), newSize);
        int end = java.lang.Math.min(xs.length,newSize);
        for (int i = 0; i < end; ++i)
            ys[i] = xs[i];
        return ys;
    }

    /**
     * Returns a copy of the specified array of integers of the
     * specified size.  As many of the original elements as will
     * fit in the new array are copied.  If the new size is longer, the
     * remaining elements will be 0.
     *
     * @param xs Original array.
     * @param newSize Length of returned array.
     * @return New array of specified length with as many elements
     * copied from the original array as will fit.
     */
    public static int[] reallocate(int[] xs, int newSize) {
        int[] ys = new int[newSize];
        int end = java.lang.Math.min(xs.length,newSize);
        for (int i = 0; i < end; ++i)
            ys[i] = xs[i];
        return ys;
    }

    /**
     * Reallocates the specified integer array to be 50 percent
     * longer, with a minimum growth in length of one element.  All of
     * the elements of the specified array will be copied into the
     * resulting array and the remaining elements initialized to zero
     * (<code>0</code>).
     *
     * @param xs Array to reallocate.
     * @return Result of reallocation.
     */
    public static int[] reallocate(int[] xs) {
        int len = (xs.length * 3) / 2;
        return reallocate(xs,len == xs.length ? xs.length + 1 : len);
    }


}

   
    
    
    
    
    
  








Related examples in the same category

1.Adds all the elements of the given arrays into a new boolean-value array.
2.Adds all the elements of the given arrays into a new byte-type array.
3.Adds all the elements of the given arrays into a new char-type array.
4.Adds all the elements of the given arrays into a new double-type array.
5.Adds all the elements of the given arrays into a new float-type array.
6.Adds all the elements of the given arrays into a new int-type array.
7.Adds all the elements of the given arrays into a new long-type array.
8.Adds all the elements of the given arrays into a new short-type array.
9.Copies the given array and adds the given element at the end of the new array. (char type value)
10.Copies the given array and adds the given element at the end of the new array. (float type value)
11.Copies the given array and adds the given element at the end of the new array. (long value type)
12.Copies the given array and adds the given element at the end of the new array. (object value type)
13.Copies the given array and adds the given element at the end of the new array.(boolean value type)
14.Copies the given array and adds the given element at the end of the new array.(byte value type)
15.Copies the given array and adds the given element at the end of the new array.(double type value)
16.Copies the given array and adds the given element at the end of the new array.(int value type)
17.Copies the given array and adds the given element at the end of the new array.(short type array)
18.Inserts the specified element at the specified position in the array.
19.Inserts the specified element at the specified position in the boolean-type-value array.
20.Inserts the specified element at the specified position in the byte-type-value array.
21.Inserts the specified element at the specified position in the char-type-value array.
22.Inserts the specified element at the specified position in the double-type-value array.
23.Inserts the specified element at the specified position in the float-value-type array.
24.Inserts the specified element at the specified position in the int-type-value array.
25.Inserts the specified element at the specified position in the long-type-value array.
26.Inserts the specified element at the specified position in the short-value-type array.
27.Removes the element at the specified position from the specified array.
28.Removes the element at the specified position from the specified long type array.
29.Removes the first occurrence of the specified element from the specified array.
30.Removes the first occurrence of the specified element from the specified long value array.
31.Insert value to array
32.Array Copy Utilities
33.Concatenate Java arrays
34.This program demonstrates array manipulation.
35.Appends an Object to an Object array.
36.Appends an integer to an integer array.
37.Inserts an Object into an Object array at the index position.
38.Prepends an Object to an Object array.
39.Removes an Object from an Object array.
40.Removes duplicate elements from the array
41.Creates a new subarray from a larger array.
42.Append one array to another
43.Array Expander
44.Array Util: seach, insert, append, remove, copy, shuffle
45.Add new value to exsisting array.The new value is indexed to the last.
46.Add new value, which sets as the first value, to existing array.
47.Add one array to another
48.Concatenates all the passed arrays
49.Pack chunks
50.Removes an element from a an array yielding a new array with that data.