更新时间:2023-02-16 来源:黑马程序员 浏览量:

要搞清楚这个问题,我们先要明白数组的概念。通常我们讲的数组是说具有相同类型的数据集合,它们一般具有固定的长度,而且在内存中占据连续的空间。在C/C++语言中,数组名只是一个指针,这个指针指向了数组的首元素,既没有属性也没有方法可以调用,而在Java语言中,数组不仅有其自己的属性(例如length属性),也有一些方法可以被调用(例如clone方法)。由于对象的特点是封装了一些数据,同时提供了一些属性和方法,从这个角度来讲,数组是对象。每个数组类型都有其对应的类型,可以通过instanceof来判断数据的类型,示例如下:
public class SubClass {
public static void main(String[] args) {
int [] a = {1,2};
int [] [] b = new int[2][4];
String [] s = {"a","b"};
if(a instanceof int[])
System out.println("the type for a is int[]");
if(b instanceof int[][])
System out.println("the type for a is int[][]");
if(s instanceof String[])
System out.println("the type for a is String[]");
}
} 程序运行的结果为:
the type for a is int[] the type for b is int[][] the type for s is String[]
1024首播|39岁程序员逆袭记:不被年龄定义,AI浪潮里再迎春天
2025-10-241024程序员节丨10年同行,致敬用代码改变世界的你
2025-10-24【AI设计】北京143期毕业仅36天,全员拿下高薪offer!黑马AI设计连续6期100%高薪就业
2025-09-19【跨境电商运营】深圳跨境电商运营毕业22个工作日,就业率91%+,最高薪资达13500元
2025-09-19【AI运维】郑州运维1期就业班,毕业14个工作日,班级93%同学已拿到Offer, 一线均薪资 1W+
2025-09-19【AI鸿蒙开发】上海校区AI鸿蒙开发4期5期,距离毕业21天,就业率91%,平均薪资14046元
2025-09-19