changes the length of the array dynamically
/** * Reallocates An-array with a new size, and copies the contents * of the ' old ' array to the new array.
* * @param oldarray the "old" array, to be reallocated.
* * @param newsize the new array size.
* * @return A new array with the same contents. * */private static object Resizearray (object oldarray, int newsize) {int oldsize = Java.lang.reflect.Array.getL
Ength (Oldarray);
Class ElementType = Oldarray.getclass (). Getcomponenttype ();
Object NewArray = java.lang.reflect.Array.newInstance (elementtype,newsize);
int preservelength = Math.min (oldsize,newsize);
if (Preservelength > 0) system.arraycopy (oldarray,0,newarray,0,preservelength); return newarray;
//Test routine for Resizearray ().
public static void Main (string[] args) {int[] a = {1,2,3};
A = (int[]) Resizearray (a,5);
A[3] = 4;
A[4] = 5; for (int i=0; i<a.length; i++) System.out.println (a[i));
}
Code is just the implementation of the basic method, detailed processing also need you to coding oh >>
Convert Array to Map
Import Java.util.Map;
Import Org.apache.commons.lang.ArrayUtils;
public class Main {public
static void Main (string[] args) {
string[][] countries = {{"United States", "New York "},
{" United Kingdom "," London "},
{" Netherland "," Amsterdam "},
{" Japan "," Tokyo "},
{" France "," Par Is '}};
Map countrycapitals = arrayutils.tomap (countries);
System.out.println ("Capital of the Japan is" + countrycapitals.get ("Japan"));
System.out.println ("Capital of the France is" + countrycapitals.get ("France"));
}