java判断是否是数组_Java判断对象类型是否为数组

  • Post author:
  • Post category:java


判断对象是否为数组:

48304ba5e6f9fe08f3fa1abda7d326ab.png

public static void main(String[] args) {

String[] a = [“1″,”2”];

if(a instanceof String[]){

System.out.println(“ss”)

}

if(a.getClass().isArray()){

System.out.println(“yy”)

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

第一种做法:instanceof

java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。

用法:

result = object instanceof class

参数:

Result:布尔类型。

Object:必选项。任意对象表达式。

Class:必选项。任意已定义的对象类。

第二种做法:Class类 isArray()

/**

* Determines if this {@code Class} object represents an array class.

*

* @return {@code true} if this object represents an array class;

* {@code false} otherwise.

* @since JDK1.1

*/

public native boolean isArray();



版权声明:本文为weixin_31481495原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。