Object Arrays
Google Guava is a java library with lot of utilities and reusable components.
This requires the library guava-10.0.jar to be in classpath.
The following example shows using ObjectArrays.newArray() API.
It returns a new array of the given length with the specified component type.
package com.bethecoder.tutorials.guava.collection_tests;
import java.util.Arrays;
import com.google.common.collect.ObjectArrays;
public class ObjectArraysTest {
/**
* @param args
*/
public static void main ( String [] args ) {
Integer [] intArray = ObjectArrays.newArray ( Integer.class, 4 ) ;
System.out.println ( Arrays.toString ( intArray ) + " length : " + intArray.length ) ;
String [] strArray = ObjectArrays.newArray ( String.class, 4 ) ;
System.out.println ( Arrays.toString ( strArray ) + " length : " + strArray.length ) ;
Long [] longOrg = new Long [] { 1L , 2L , 3L , 4L } ;
Long [] longArray = ObjectArrays.newArray ( longOrg, 2 ) ;
System.out.println ( Arrays.toString ( longArray ) + " length : " + longArray.length ) ;
}
}
It gives the following output,
[null, null, null, null] length : 4
[null, null, null, null] length : 4
[null, null] length : 2