Use generics to expand java arrays and java Arrays

Source: Internet
Author: User

Use generics to expand java arrays and java Arrays

Compile a common method. Its function is to extend the array to 10% + 10 elements (repost the source)

1 package cn. reflection; 2 3 import java. lang. reflect. array; 4 5 public class ArrayGrowTest {6 public static void main (String [] args) {7 ArrayGrowTest growTest = new ArrayGrowTest (); 8 int [] aInt = {1, 2, 3, 4}; 9 System. out. print ("original array:"); 10 growTest. printArray (aInt); 11 aInt = (int []) growTest. goodArrayGrow (aInt); 12 System. out. print ("expanded array:"); 13 growTest. printArray (aInt); 14 15 String [] aStr = {"hello", "world "," Ni "," hao "," ma "}; 16 System. out. print ("original array:"); 17 growTest. printArray (aStr); 18 aStr = (String []) growTest. goodArrayGrow (aStr); 19 System. out. print ("expanded array:"); 20 growTest. printArray (aStr); 21} 22/** 23 * array resizing method, support for different array types 24 * @ param a original array 25 * @ return newArray new array 26 */27 public Object goodArrayGrow (Object a) {28 Class cl =. getClass (); 29 if (! Cl. isArray () {30 return null; 31} 32 Class componentType = cl. getComponentType (); // use the getComponentType method of the Class to determine the type of the Array. 33 int length = Array. getLength (a); // The original length 34 int newLength = length * 11/10 + 10; // The New length 35 Object newArray = Array. newInstance (componentType, newLength); // instance the new array 36 // assign a value to the new array. a indicates the original array. The first 0 indicates the start position of the original array copy, and newArray indicates the new array, the second 0 represents the starting position of the new array value, and the length represents the length from the original array to the new array 37 System. arraycopy (a, 0, newArray, 0, length ); 38 return newArray; 39} 40/** 41 * output array content 42 * @ param a output array 43 */44 public void printArray (Object) {45 Class cl =. getClass (); 46 if (! Cl. isArray () {47 return; 48} 49 Class componentType = cl. getComponentType (); 50 int length = Array. getLength (a); 51 System. out. print (componentType. getName () + "[" + length + "] = {"); 52 for (int I = 0; I <length; I ++) {53 System. out. print (Array. get (a, I) + ""); 54} 55 System. out. println ("}"); 56} 57 58}

 

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.