什么是最终的ArrayList的意义吗? [英] what is the sense of final ArrayList?

查看:160
本文介绍了什么是最终的ArrayList的意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些优点/缺点我们可以做的ArrayList(或其他集合),最终得到什么?我还可以添加到ArrayList的新元素,删除元素和更新。但什么是影响使得它的决赛?


解决方案

  

但什么是影响使得它最终的?


这意味着你不能重新绑定变量指向一个不同的集合实例的:

 最终名单,LT;整数GT;名单=新的ArrayList<整数GT;();
名单=新的ArrayList<整数GT;(); //由于'名单'是决赛,这将不编译

作为一个风格问题,我宣布,我不打算改变,因为大多数引用最后


  

我还可以添加到ArrayList的新元素,删除元素和更新。


如果你愿意,你可以通过使用<一prevent插入,删除等href=\"http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#unmodifiableList%28java.util.List%29\"><$c$c>Collections.unmodifiableList():

 最终名单,LT;整数GT;清单= Collections.unmodifiableList(新的ArrayList&LT;整数GT;(...));

Which advantages/disadvantages we can get by making ArrayList (or other Collection) final? I still can add to ArrayList new elements, remove elements and update it. But what is effect making it's final?

解决方案

But what is effect making it's final?

This means that you cannot rebind the variable to point to a different collection instance:

final List<Integer> list = new ArrayList<Integer>();
list = new ArrayList<Integer>(); // Since `list' is final, this won't compile

As a matter of style, I declare most references that I don't intend to change as final.

I still can add to ArrayList new elements, remove elements and update it.

If you wish, you can prevent insertion, removal etc by using Collections.unmodifiableList():

final List<Integer> list = Collections.unmodifiableList(new ArrayList<Integer>(...));

这篇关于什么是最终的ArrayList的意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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