在 Java 中为 ArrayList 项分配 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4028330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning null to ArrayList items in Java
提问by foamboarder
I have the following code:
我有以下代码:
static ArrayList<PreparedStatement> alPrepStmts = new ArrayList<PreparedStatement>();
static PreparedStatement m_stmtOne;
static PreparedStatement m_stmtTwo;
static PreparedStatement m_stmtThree;
...
static PreparedStatement m_stmtOneHundred;
private static void init() {
alPrepStmts.add(m_stmtOne);
alPrepStmts.add(m_stmtTwo);
alPrepStmts.add(m_stmtThree);
...
alPrepStmts.add(m_stmtOneHundred);
}
private static void work() {
if(m_stmtOne == null) {
// assign m_stmtOne
}
// use m_stmtOne
}
private static void close() throws SQLException {
for(PreparedStatement ps : alPrepStmts) {
if(ps != null) {
ps.close();
ps = null;
}
}
}
init() gets called once. work() and close() may get called several times. The problem is that after calling close(), the PreparedStatements are not set to null. The next time I call work(), m_stmtOne is not null, but is closed. I supposed I can check if m_stmtOne is open, but I am wondering how can I assign null to the members of the container.
init() 被调用一次。work() 和 close() 可能会被多次调用。问题是在调用 close() 之后,PreparedStatements 没有设置为 null。下次我调用 work() 时,m_stmtOne 不为空,而是关闭了。我想我可以检查 m_stmtOne 是否打开,但我想知道如何将 null 分配给容器的成员。
I also tried using iterators and it does not work either. The if conditional below is never true.
我也尝试使用迭代器,但它也不起作用。下面的 if 条件永远不会为真。
private static void ClosePreparedStatements() throws SQLException {
for(Iterator<PreparedStatement> it = alPrepStmts.iterator(); it.hasNext();) {
PreparedStatement ps = (PreparedStatement)it.next();
if(ps != null) {
ps.close();
ps = null;
}
}
}
I know I can close and assign every statement manually, but I'm just wondering what is the best way to assign null to elements in a container.
我知道我可以手动关闭和分配每个语句,但我只是想知道将 null 分配给容器中的元素的最佳方法是什么。
回答by Keith Randall
Your assignment ps = null
assigns only the local variable, it does not modify the original container. You should use the remove
method of java.util.Iterator
(in your second code snipped instead of ps = null
) to remove an element from the container. Note this removes the element from the container, not replace it with null in the container.
您的赋值ps = null
仅分配局部变量,它不会修改原始容器。您应该使用remove
的方法java.util.Iterator
(在剪断的,而不是你的第二个代码ps = null
),从容器中取出的元素。请注意,这会从容器中删除元素,而不是将其替换为容器中的 null。
Even if you remove the element from the container, it will not change the value of, e.g. m_stmtOne
, which it sounds like you want to have happen also. You will have to do that some other way.
即使您从容器中移除元素,它也不会改变例如的值,m_stmtOne
听起来您也希望发生这种情况。你将不得不以其他方式做到这一点。
It also seems init()
just adds 100 nulls to your list of prepared statements. Assigning a non-null value to m_stmtOne
later will not change the list. In other words, m_stmtOne
gets added to the list by value, not by reference.
它似乎也init()
只是在准备好的语句列表中添加了 100 个空值。将非空值分配给m_stmtOne
以后不会更改列表。换句话说,m_stmtOne
通过值而不是引用添加到列表中。
回答by Christian Mann
One way would be:
一种方法是:
for(int i = 0; i < alPrepStmts.size(); i++) {
if(alPrepStmts.get(i) != null) {
alPrepStmts.get(i).close()
alPrepStmts.set(i, null);
}
}
Incidentally, if you have a List<PreparedStatement>
, you don't need to cast each entry to PreparedStatement
. That's what Java Generics are for in the first place.
顺便说一句,如果您有List<PreparedStatement>
,则无需将每个条目强制转换为PreparedStatement
。这就是 Java 泛型的最初用途。
回答by Matti Lyra
java.util.Collections.fill(List<? super T> list, T obj);
回答by kaliatech
The for statement is creating a locally scoped variable. You set that null, but that does not set the list element or class member variable reference null, as you've probably realized.
for 语句正在创建一个局部范围的变量。您设置了 null,但这并没有将列表元素或类成员变量引用设置为 null,正如您可能已经意识到的那样。
EDIT:The more I look at this code , the more I don't understand what it is meant to do.
编辑:我越看这段代码,就越不明白它的意思。