Java examples for Collection Framework:Array Element
Creates an array with a single object as component.
//package com.java2s; import java.lang.reflect.Array; public class Main { public static void main(String[] argv) throws Exception { Object object = "java2s.com"; System.out.println(convertToArray(object)); }/* w w w .j a v a2 s .c o m*/ /** * Creates an array with a single object as component. * If the specified object is an array, it will be returned * unchanged. Otherwise an array with the specified object * as the single element will be returned. * @param object the object * @return the corresponding array */ public static Object convertToArray(Object object) { if (object.getClass().isArray()) return object; Object array = Array.newInstance(object.getClass(), 1); Array.set(array, 0, object); return array; } }