Code instance sharing _java dynamically changing array length and array to map in Java

Source: Internet
Author: User

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"));    
}   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.