Append item to array
import java.lang.reflect.Array;
public final class Main
{
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] oldArray, T toAppend)
{
Class<?> component = oldArray.getClass().getComponentType();
T[] array = (T[])Array.newInstance(component, oldArray.length+1);
System.arraycopy(oldArray, 0, array, 0, oldArray.length);
array[oldArray.length] = toAppend;
return array;
}
/**
* Copies the elements of the {@code oldArray} to a new array with extra space to
* append the given element {@code toAppend1} and the array {@code toAppend2}.
*/
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] oldArray, T toAppend1, T[] toAppend2)
{
Class<?> component = oldArray.getClass().getComponentType();
T[] array = (T[])Array.newInstance(component,
oldArray.length + 1 + toAppend2.length);
System.arraycopy(oldArray, 0, array, 0, oldArray.length);
array[oldArray.length] = toAppend1;
System.arraycopy(toAppend2, 0, array, oldArray.length+1, toAppend2.length);
return array;
}
/**
* Copies the elements of the {@code oldArray} to a new array with extra space to
* append the given array {@code toAppend}.
*/
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] oldArray, T[] toAppend)
{
Class<?> component = oldArray.getClass().getComponentType();
T[] array = (T[])Array.newInstance(component,
oldArray.length + toAppend.length);
System.arraycopy(oldArray, 0, array, 0, oldArray.length);
System.arraycopy(toAppend, 0, array, oldArray.length, toAppend.length);
return array;
}
/**
* Copies the elements of the {@code oldArray} to a new array with extra space to
* append the given array {@code toAppend1} and the element {@code toAppend2}.
*/
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] oldArray, T[] toAppend1, T toAppend2)
{
Class<?> component = oldArray.getClass().getComponentType();
T[] array = (T[])Array.newInstance(component,
oldArray.length + toAppend1.length + 1);
System.arraycopy(oldArray, 0, array, 0, oldArray.length);
System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length);
array[array.length-1] = toAppend2;
return array;
}
/**
* Copies the elements of the {@code oldArray} to a new array with extra space to
* append the given array {@code toAppend1} and the array {@code toAppend2}.
*/
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] oldArray, T[] toAppend1, T[] toAppend2)
{
Class<?> component = oldArray.getClass().getComponentType();
T[] array = (T[])Array.newInstance(component,
oldArray.length + toAppend1.length + toAppend2.length);
System.arraycopy(oldArray, 0, array, 0, oldArray.length);
System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length);
System.arraycopy(toAppend2, 0, array, oldArray.length + toAppend1.length, toAppend2.length);
return array;
}
/**
* Returns a copy of the old array {@code oldArray} but with the element at the
* give index {@code idx} removed.
*
* @throws {@link IllegalArgumentException} if the array index is out of bounds.
*/
@SuppressWarnings("unchecked")
public static <T> T[] remove(T[] oldArray, int idx)
{
if(idx<0 || idx>=oldArray.length)
throw new IllegalArgumentException("array index " + idx + " out of bounds");
Class<?> component = oldArray.getClass().getComponentType();
T[] array = (T[])Array.newInstance(component, oldArray.length-1);
if(idx==0)
System.arraycopy(oldArray, 1, array, 0, array.length);
else
{
System.arraycopy(oldArray, 0, array, 0, idx);
System.arraycopy(oldArray, idx+1, array, idx, array.length-idx);
}
return array;
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Data Type Array:
- Append item to array
- Append an object to an array.
- Append one array to another
- Clone Array
- Clones two dimensional float array
- Compare two arrays by reference
- Compare Two byte Arrays
- Compare Two boolean Arrays
- Compare Two char Arrays
- Compare Two double Arrays
- Compare Two float Arrays
- Compare Two int Arrays
- Compare Two long Arrays
- Compare two object arrays
- Compare Two short Arrays
- Compare two two-dimensional array
- Concatenate arrays
- Convert array to set
- Convert string array to string
- Convert string array to List
- Convert multi-dimensional array to string
- Convert an array to a Map
- Copy array
- Copy Elements from One Array to Another
- Copy and add an array at the end of the new array
- Extend an array with additional extra space
- Extend to Double the size of an array
- Fill boolean, byte, char,short, int, long, float array
- Get array upperbound
- Get the number of dimensions
- Insert an Element into a Sorted Array
- Insert the specified element at the specified position in the array
- Insert source array in the target array at offset
- Maximum value in an array
- Minimum value in an array.
- Merge two arrays
- Remove duplicate element from array
- Remove the element at the specified position from the specified array.
- Reverses the order of an array(byte, long, int)
- Search an element in an array and return the index and last index
- Binary Search on an Array
- Shift all elements right by one
- Shift all elements left by one
- Shuffle an array
- Sort byte(char,double, float, int) Array
- Sort char array
- Sort int Array
- Sort long Array
- Sort short Array
- Sort string type array in case insensitive order and case sensitive order
- Sort an Array in Descending (Reverse) Order
- Start with one array
- Subarray from array that starts at offset