为什么Java的允许大小为0的数组? [英] Why does Java allow arrays of size 0?

查看:333
本文介绍了为什么Java的允许大小为0的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不像C,在那里你可以动态增加数组的大小,Java数组是固定长度。 Java的为什么让大小为0则数组?

 的String [] =串新的String [0];


解决方案

这意味着它是空的。即你可以遍历它,就好像它有项目,有没有结果出现:

 的for(int k = 0; K< strings.length; k ++){
   //东西
}

由此避免需要进行检查。如果有问题的阵列均,会出现异常,但在这种情况下,它只是什么也不做,这可能是合适的。

Unlike in C, where you can dynamically increase the size of an array, arrays in java are fixed in length. Why does Java allow arrays of size 0 then?

String[] strings = new String[0];

解决方案

It signifies that it is empty. I.e. you can loop over it as if it had items and have no result occur:

for(int k = 0; k < strings.length; k++){
   // something
}

Thereby avoiding the need to check. If the array in question were null, an exception would occur, but in this case it just does nothing, which may be appropriate.

这篇关于为什么Java的允许大小为0的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆