Java method to determine whether it is an array:
1. Use instanceof to determine whether it is an array
instanceof is Java's A binary operator, similar to operators such as ==, >, <.
instanceof is a reserved keyword in Java. Its function is to test whether the object on its left is an instance of the class on its right and return the data type of boolean.
public List findByProperty(String propertyName, Object value) { List list = new ArrayList(); String queryString = "from Userinfo as model where model." + propertyName + "= ? order by model.userInfoId DESC"; try { // //判断是否为String数组类型 if ( value instanceof String[] ){ //如果为true则强转成String数组 String [] arr = ( String[] ) value ; for ( int i = 0 ; i < arr.length ; i++ ){ this.getHibernateTemplate().find(queryString, value); list.add(this.getHibernateTemplate().find(queryString, arr[i])); } }else{ list = this.getHibernateTemplate().find(queryString, value); } } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return list; }
2. Use getClass()
getClass() to get the type of the object. Based on the obtained object type, determine whether it is an Array array
/** * 对象是否为数组对象 * * @param obj 对象 * @return 是否为数组对象,如果为{@code null} 返回false */ public static boolean isArray(Object obj) { if (null == obj) { // throw new NullPointerException("Object check for isArray is null"); return false; } // 反射 获得类型 return obj.getClass().isArray(); }
For more java knowledge, please pay attention to java basic tutorial.
The above is the detailed content of Determine whether it is an array in java. For more information, please follow other related articles on the PHP Chinese website!