Home > Java > JavaBase > Determine whether it is an array in java

Determine whether it is an array in java

Release: 2019-11-22 10:47:40
Original
5986 people have browsed it

Determine whether it is an array in java

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;
	}
Copy after login

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();
  }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template