将 ArrayList 对象声明为最终对象以在常量文件中使用
我正在生成一个对象的 ArrayList。 以下是代码
ArrayList someArrayList = new ArrayList();
Public ArrayList getLotOfData()
{
ArrayList someData = new ArrayList();
return someData;
}
someArrayList = eDAO.getLotOfData();
一旦我有了这个 ArrayList 对象“someArrayList”,我想将其声明为 public 和 Final 并将其存储在 Constants 文件中,以便可以全局访问它。 我有办法做到这一点吗?如果我将 Arraylist 对象声明为 public 和 Final,那么我将无法为其重新分配任何值。 我尝试了以下方法,
public final ArrayList anotherArrayList = new ArrayList();
anotherArrayList.addAll(someArrayList);
我希望将这个“anotherArrayList”存储为全局 ArrayList 对象并使用它,但这会返回空指针异常。我想像字符串常量“ConstantsFile.anotherArrayList”一样使用它。有什么想法吗???
I am generating an ArrayList of objects.
Following is the code
ArrayList someArrayList = new ArrayList();
Public ArrayList getLotOfData()
{
ArrayList someData = new ArrayList();
return someData;
}
someArrayList = eDAO.getLotOfData();
Once I have this ArrayList object "someArrayList", I would like to declare it public and final and store it in a Constants file so that it can be accessed globally.
Is there a way I can do that? If I declare an Arraylist object public and final, then I would not be able to reassign any values to it.
I tried the following
public final ArrayList anotherArrayList = new ArrayList();
anotherArrayList.addAll(someArrayList);
I had hoped to store this "anotherArrayList" as a global ArrayList object and use it, but this returns a nullpointer exception. I want to use it just like a String constant "ConstantsFile.anotherArrayList". Any ideas???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以轻松地将其设为
public static final
,但这不会阻止人们更改内容。最好的方法是安全地发布“常量”,方法是:
,从而产生一个带有初始化的整洁的最终声明:
或者,类似但不同的风格以实现简单元素(不需要代码)
You can easily make it
public static final
, but that won't stop people from changing the contents.The best approach is to safely publish the "constant" by:
Resulting in one neat final declaration with initialization:
or, similar but different style for simple elements (that don't need code)
Guava 提供
ImmutableList
正是出于这个原因。 (此外,它没有ArrayList
分配的不必要的空间开销,以便为将来不会为应用程序添加的元素腾出空间。)Guava provides
ImmutableList
for just about this reason. (Also, it doesn't have the unnecessary space overhead thatArrayList
allocates to make room for future elements which you won't be adding for your application.)Java 1.4兼容方式:
此类List是不可修改的,调用
add()
、remove()
或set()
等方法会导致不支持的操作异常
。对于不太古老的 Java 版本:
最后,Java 9 附带:
Java 1.4 compatible way:
Such List is unmodifiable, calling methods such as
add()
,remove()
orset()
will causeUnsupportedOperationException
.For less ancient Java versions:
And finally, Java 9 comes with:
从 Java 9 开始,解决方案非常简单。
您甚至不需要番石榴,因为我们有:
Since Java 9 the solution is super simple.
You do not even need Guava, since we have:
这是创建最终 ArrayList 并使其不可修改的另一种方法。它涉及创建一个类来保存最终的 ArrayList。
首先,列出你的清单,完成你需要做的所有突变。
其次,创建一个类,该类将在其构造函数中创建最终的 ArrayList。
当您需要带有列表的不可变类时,这非常有用。
Here is another way you can create a final ArrayList and then make it unmodifiable. It involves creating a class to hold the final ArrayList.
First, make your list, doing all the mutations you need to do.
Second, create a class that will create the final ArrayList in its constructor.
This is useful for when you need an immutable class with a List.