Java Final arraylist [英] Java Final arraylist

查看:159
本文介绍了Java Final arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是宣布arraylist为final。我知道,一旦我写了最后的 ArrayList list = new ArrayList(); 我可以添加,删除此列表中的对象,但我不能 list = new ArrayList() list = list1 。但是使用声明arraylist作为
私有静态final ArrayList list = new ArrayList(); 。除了我上面提到的差异之外,以下两个声明之间的区别是什么:

My question is regarding declaring an arraylist as final. I know that once I write final ArrayList list = new ArrayList(); I can add, delete objects from this list, but I can not list = new ArrayList() or list = list1. But what will be the use of declaring arraylist as Private static final ArrayList list = new ArrayList();. And apart from the difference I have mentioned above what will be the difference between following two declaration:

1. ArrayList list = new ArrayList()

2. private static final ArrayList list =  new ArrayList();


推荐答案

只是为了为你的工厂带点水当你想让你的名单公开可用但不可修改时,你会理解 final 的利益。

Just to "bring a little water to your Mill" you will understand the interest of final when you'll want to make your list publically availiable but unmodifiable.

In java可以使用 Collections.unmodifiableList(modifiableList)

In java one can make a list unmodifiable with Collections.unmodifiableList(modifiableList).

现在看一下以下代码:

public class MyClass{
  public static List<String> MY_PUBLIC_LIST;
  static{
    ArrayList<String> tmp = new ArrayList<String>();
    tmp.add("a");
    tmp.add("b");
    tmp.add("c");
    MY_PUBLIC_LIST = tmp;
  }
}

嗯,在任何类中,代码中的任何位置都可以做这样的事情

Well, in anyclass, anywhere in your code you can do something like this

MyClass.MY_PUBLIC_LIST = null;
MyClass.MY_PUBLIC_LIST = new ArrayList<String>();
MyClass.MY_PUBLIC_LIST.clear();
MyClass.MY_PUBLIC_LIST.add("1");

当您将 final 关键字添加到您的变量,前两个将不被允许

When you add the final keyword to your variable, the first two won't be allowed

public static final List<String> MY_PUBLIC_LIST;

但您仍然可以修改列表内容:

But you'll still be able to modify the content of the list :

MyClass.MY_PUBLIC_LIST.clear();
MyClass.MY_PUBLIC_LIST.add("1");

添加 Collections.unmodifiableList(modifiableList)在静态块的末尾你'我也会阻止这种情况:

By adding a Collections.unmodifiableList(modifiableList) at the end of the static block you'll prevent this too :

MY_PUBLIC_LIST = Collections.unmodifiableList(tmp);

好的,我们差不多了。为了确保你得到全貌,请保留 Collections.unmodifiableList(modifiableList)但让我删除最终修饰符

Ok we are almost there. Just to be sure you get the whole picture lets keep the Collections.unmodifiableList(modifiableList) but let me remove the final modifier

public class MyClass{
  public static List<String> MY_PUBLIC_LIST;
  static{
    ArrayList<String> tmp = new ArrayList<String>();
    tmp.add("a");
    tmp.add("b");
    tmp.add("c");
    MY_PUBLIC_LIST = Collections.unmodifiableList(tmp);
  }
}

在这种情况下你能做什么?

What can you do in that case ?

...

...

嗯,你可以在第一种情况下做任何你想做的事情(假设你先分配新列表):

Well you can do whatever you want like in the first case (given that you assign the new list first) :

MyClass.MY_PUBLIC_LIST = null;
MyClass.MY_PUBLIC_LIST = new ArrayList<String>();
MyClass.MY_PUBLIC_LIST.clear();
MyClass.MY_PUBLIC_LIST.add("1");

这篇关于Java Final arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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