如何清除ArrayList? [英] How do I clear an ArrayList?

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

问题描述

我在 ThisClass 中有以下代码:

static ArrayList<MyClass> classlist; 

如果我致电:

ThisClass.classlist = new ArrayList<MyClass>();
ThisClass.classlist.add(object);

然后再次拨打此行:

ThisClass.classlist = new ArrayList<MyClass>();

是否会重置 ThisClass.classlist 列表,即类列表列表将不再包含对象?

Will it reset the ThisClass.classlist list, i.e. the classlist list will no longer contain object?

推荐答案

以下是插图:

ThisClass.classlist = new ArrayList<MyClass>();
ThisClass.classlist.add(object);

结果如下:

ThisClass.classlist = new ArrayList<MyClass>();

结果到此 - 您通过将其指向一个新对象来重置它:

Results into this - you're resetting it by making it point to a fresh object:

如何使其不再包含对象应该做的是:

What you should do to make it "no longer contain an object" is:

ThisClass.classlist.clear();

清除所有元素的循环并使它们 null 。在内部,ArrayList也指向其对象的内存地址,但为简单起见,只要认为它们在调用此方法时被删除。

Clear loops through all elements and makes them null. Well internally the ArrayList also points to the memory address of its objects, but for simplicity, just think that they're being "deleted" when you call this method.

< img src =https://i.stack.imgur.com/BXuqK.pngalt =在此输入图像说明>

如果你想让它不再包含ArrayList,你可以:

If you want to make it "no longer contain an ArrayList" you do:

ThisClass.classlist = null;

这意味着:

另外,请注意你的问题是title提到静态ArrayList。 static 在此上下文中无关紧要。无论对象是否为静态,问题的结果都是相同的。

Also, take note that your question's title mentions "static ArrayList". static doesn't matter in this context. The result of your problem will be the same whether the object is static or not.

这篇关于如何清除ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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