将 ArrayList 对象声明为最终对象以在常量文件中使用

发布于 2025-01-05 08:22:09 字数 659 浏览 1 评论 0原文

我正在生成一个对象的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

早茶月光 2025-01-12 08:22:09

您可以轻松地将其设为public static final,但这不会阻止人们更改内容

最好的方法是安全地发布“常量”,方法是:

  • 将其包装在不可修改的列表中以填充它
  • 使用实例块

,从而产生一个带有初始化的整洁的最终声明:

public static final List<String> list = Collections.unmodifiableList(
    new ArrayList<String>() {{
        add("foo");
        add("bar");
        // etc
    }});

或者,类似但不同的风格以实现简单元素(不需要代码)

public static final List<String> list = 
    Collections.unmodifiableList(Arrays.asList("foo", "bar"));

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:

  • wrapping it in an unmodifiable list
  • using an instance block to populate it

Resulting in one neat final declaration with initialization:

public static final List<String> list = Collections.unmodifiableList(
    new ArrayList<String>() {{
        add("foo");
        add("bar");
        // etc
    }});

or, similar but different style for simple elements (that don't need code)

public static final List<String> list = 
    Collections.unmodifiableList(Arrays.asList("foo", "bar"));
请止步禁区 2025-01-12 08:22:09

Guava 提供ImmutableList 正是出于这个原因。 (此外,它没有 ArrayList 分配的不必要的空间开销,以便为将来不会为应用程序添加的元素腾出空间。)

public static final ImmutableList<String> CONSTANTS = 
  ImmutableList.of("foo", "bar");

Guava provides ImmutableList for just about this reason. (Also, it doesn't have the unnecessary space overhead that ArrayList allocates to make room for future elements which you won't be adding for your application.)

public static final ImmutableList<String> CONSTANTS = 
  ImmutableList.of("foo", "bar");
任谁 2025-01-12 08:22:09

Java 1.4兼容方式:

public static final List STRINGS = Collections.unmodifiableList(
    Arrays.asList(new String[] {"foo", "bar"}));

此类List是不可修改的,调用add()remove()set()等方法会导致不支持的操作异常

对于不太古老的 Java 版本:

public static final List<String> STRINGS = Collections.unmodifiableList(
    Arrays.asList("foo", "bar"));

最后,Java 9 附带:

public static final List<String> STRINGS = List.of("foo", "bar");

Java 1.4 compatible way:

public static final List STRINGS = Collections.unmodifiableList(
    Arrays.asList(new String[] {"foo", "bar"}));

Such List is unmodifiable, calling methods such as add(), remove() or set() will cause UnsupportedOperationException.

For less ancient Java versions:

public static final List<String> STRINGS = Collections.unmodifiableList(
    Arrays.asList("foo", "bar"));

And finally, Java 9 comes with:

public static final List<String> STRINGS = List.of("foo", "bar");
一抹微笑 2025-01-12 08:22:09

从 Java 9 开始,解决方案非常简单。

您甚至不需要番石榴,因为我们有:

    public static final List<String> list = List.of("foo", "bar");

Since Java 9 the solution is super simple.

You do not even need Guava, since we have:

    public static final List<String> list = List.of("foo", "bar");
飘过的浮云 2025-01-12 08:22:09

这是创建最终 ArrayList 并使其不可修改的另一种方法。它涉及创建一个类来保存最终的 ArrayList。

首先,列出你的清单,完成你需要做的所有突变。

其次,创建一个类,该类将在其构造函数中创建最终的 ArrayList。

public class Wrapper {

    final List<T> list;
   
    // Other final variables to be set and added to the constructor.

    public Wrapper(List<T> list) {
        this.list = Collections.unmodifiableList(list);
    }

}

当您需要带有列表的不可变类时,这非常有用。

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.

public class Wrapper {

    final List<T> list;
   
    // Other final variables to be set and added to the constructor.

    public Wrapper(List<T> list) {
        this.list = Collections.unmodifiableList(list);
    }

}

This is useful for when you need an immutable class with a List.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文