Java determines whether an object is an array and gets the value of the arrays---using instanceof or IsArray () method

Source: Internet
Author: User

Today's project logic needs to pass the number of groups past. Then a generic method is invoked. But the parameter of this method is object. If you change the parameter type, the other module calls this method and there is a problem.

The final solution is to add a judgment to the DAO layer

This is the DAO layer code

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 {
////			judge if string array type
			if (value instanceof   string[]) {
				///If True to strong to string array
				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;
	}

In addition to instanceof to judge can also use the Istarray method
if (Value.getclass (). IsArray ()) {
   //If it is an array then action
}
Online search looked down they all suggested using the IsArray method to say that the first one could be an anomaly.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.