为什么ArrayList没有getSize()而不是size()? [英] Why doesn't ArrayList have getSize() instead of size()?

查看:95
本文介绍了为什么ArrayList没有getSize()而不是size()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些JSP页面中,我大量使用ArrayList.我想像这样访问一个ArrayList:

I'm making heavy use of ArrayList in some JSP pages. I would like to access an ArrayList like so:

${myArrayList.size}

但是由于对象必须符合JavaBean标准(在JSP/JSTL中myArrayList.getMyPropertyName()${myArrayList.myPropertyName}),所以无法访问myArrayList.size()函数.

But since objects must to conform to the JavaBean standard, where myArrayList.getMyPropertyName() is ${myArrayList.myPropertyName} in JSP/JSTL, the myArrayList.size() function is not accessible.

还有其他我应该使用的课程吗?

Is there another class I should be using?

更新:

很难接受这样的功能,例如仅在较干净的衬板上保留JSP中的ArrayList的大小之类的功能,但是似乎还有其他原因导致设计师选择了.size()而不是.getSize()

It's a bit difficult to accept that a feature such as getting the size of your ArrayList in JSP is left out just for cleaner one liners but it seems there can be other reasons the designers chose .size() instead of .getSize()

我想如果您支持${fn:length(myArrayList)}${myArrayList.getSize()}语法,这真的不是问题.

I guess this really isn't a problem if you have support for ${fn:length(myArrayList)} or ${myArrayList.getSize()} syntax.

推荐答案

标题说明了一切:为什么ArrayList没有getSize()而不是size()?

The title says it all: Why doesn't ArrayList have getSize() instead of size()?

如" Java Collections API设计中所述常见问题解答":

为什么不使用"Beans样式名称"来保持一致性?

新集合方法的名称不遵循 在"Beans命名约定"中,我们认为它们是 合理,一致且适合其目的.它应该 请记住,Beans命名约定不适用于 整个JDK; AWT确实采用了这些约定,但是 这个决定有些争议.我们怀疑 集合API将被广泛使用,通常用于 在一行代码上有多个方法调用,因此这很重要 名称简短.考虑例如Iterator 方法.当前,对集合的循环如下所示:

While the names of the new collections methods do not adhere to the "Beans naming conventions", we believe that they are reasonable, consistent and appropriate to their purpose. It should be remembered that the Beans naming conventions do not apply to the JDK as a whole; the AWT did adopt these conventions, but that decision was somewhat controversial. We suspect that the collections APIs will be used quite pervasively, often with multiple method calls on a single line of code, so it is important that the names be short. Consider, for example, the Iterator methods. Currently, a loop over a collection looks like this:

    for (Iterator i = c.iterator(); i.hasNext(); )
        System.out.println(i.next());

即使集合名称为 一个很长的表情.如果我们将方法命名为"getIterator", "hasNextElement"和"getNextElement",它将不再是 案子.因此,我们采用了传统的" JDK样式,而不是 豆子风格.

Everything fits neatly on one line, even if the Collection name is a long expression. If we named the methods "getIterator", "hasNextElement" and "getNextElement", this would no longer be the case. Thus, we adopted the "traditional" JDK style rather than the Beans style.

这篇关于为什么ArrayList没有getSize()而不是size()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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