零长度的阵列的使用 [英] Use of array of zero length

查看:148
本文介绍了零长度的阵列的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我们可以构建这样一个数组是这样的:

For example we can construct such an array like this:

 new ElementType[0];

我见过这样的概念,但是我不明白为什么这可能是有用的。

I seen such a construct, but I don't understand why this might be useful.

推荐答案

这是例子。我说,你有一个函数

An example. Say, you have a function

public String[] getFileNames(String criteria) {

得到一些文件名。试想一下,你没有找到满足条件的任何文件名。你有什么回报?你有两个选择 - 要么返回空 0大小的数组的。

0大小的数组的变体的更好,因为你的调用者不需要检查 NULL 并能以一致的方式处理阵列 - 比如,在一个循环中(这将是在这种情况下,空的)。

The variant with 0-sized array is better, because your caller doesn't need to check for NULL and can process the array in a consistent way - say, in a loop (which would be empty in this case).

有在<一个在这一个章节href=\"http://books.google.com/books?id=ZZOiqZQIbRMC&lpg=PP1&dq=effective%2Bjava&pg=PA134\"相对=nofollow>有效的Java,项目27

这篇关于零长度的阵列的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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