Source Code : Array Expander

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Array Expander

     
//package com.witframework.util;

import java.lang.reflect.Array;

public final class ArrayExpander {
  public static Object merge(Object array1,Object array2){
    if (array1 == null) {
      return null;
    }
    if (array2 == null) {
      return array1;
    }
    Class c = array1.getClass();
    if(c.isArray()&&array2.getClass().isArray()){
      Class cc = c.getComponentType();
      Object newArray = Array.newInstance(cc, Array.getLength(array1)+Array.getLength(array2));
      System.arraycopy(array1, 0, newArray, 0, Array.getLength(array1));
      System.arraycopy(array2, 0, newArray, Array.getLength(array1), Array.getLength(array2));
      return newArray;
    }else {
      throw new ClassCastException("need  array");
    }
    
  }
  public static Object expand(Object array, int newSize) {
    if (array == null) {
      return null;
    }
    Class c = array.getClass();
    if (c.isArray()) {
      int len = Array.getLength(array);
      if (len >= newSize) {
        return array;
      } else {
        Class cc = c.getComponentType();
        Object newArray = Array.newInstance(cc, newSize);
        System.arraycopy(array, 0, newArray, 0, len);
        return newArray;
      }
    } else {
      throw new ClassCastException("need  array");
    }
  }
  public static Object expandAtHead(Object array, int newSize) {
    if (array == null) {
      return null;
    }
    Class c = array.getClass();
    if (c.isArray()) {
      int len = Array.getLength(array);
      if (len >= newSize) {
        return array;
      } else {
        Class cc = c.getComponentType();
        Object newArray = Array.newInstance(cc, newSize);
        System.arraycopy(array,0, newArray, newSize-len, len);
        return newArray;
      }
    } else {
      throw new ClassCastException("need  array");
    }
  }  
}

   
    
    
    
    
  

Thank with us