search
HomeJavaJavaBaseDetermine whether it is an array in java

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;
	}

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools