java数组后面 是什么意思_Java数组是同构的是什么意思,但ArrayLists不是?

是.Java Arrays是同构的,因为当您在Java中声明任何数组时,您必须声明其type。

例如:

int arr[]; //type is int

String arr[]; //type is String

float arr[]; //type is float现在,如果您尝试在声明的数组中存储任何其他数据类型,则会出现编译时错误。

例如:

int arr=new int[5];

arr[0]="I am a String not int"; //compile time error但ArrayList是Collection的一部分,他们持有Objects,而不是任何具体data-type [如果我们不是在谈论generics],因为在Java中的每一件事情,直接或间接地从Object class继承的,因此它不会给你compile-time error,type检查将在run-time上。

例如:

ArrayList al=new ArrayList();//type is Object

al.add("I am a String"); //Bacause String class in inherited from Object Class

al.add(1);//the int 1 will first autobox into Integer class then stored in al ArrayList.Now bacause Integer class is also inherited from Object class,it will*/ allow you to store

al.add(UserDefinedClass); //because every User defined class is also inherited from Object class,so it will also allow you.现在你注意到了,因为我们还没有定义ArrayList al的任何数据类型,但我们仍然存储不同的类型值:这就知道为什么ArrayList存储Object不是特定的数据类型,因此它们是异构的而不是同类的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值