如何检查Vector是否存在且是否已在Java中定义?

时间:2021-07-10 07:20:23

I'm attempting to check whether vector.isEmpty() before looping through it but sometimes the vector hasn't even been defined yet because it gets assigned within a function somewhere else. Is there a way to check whether vector == Vector before trying to invoke any Vector methods on said vector?

我试图在循环之前检查vector.isEmpty(),但有时甚至还没有定义向量,因为它在其他地方的函数中被分配。有没有办法在尝试调用所述向量上的任何Vector方法之前检查vector == Vector?

I'm coming from a JavaScript background and because of its loosely typed system, I can simply do if (isArray(array)). Is there any similar or standard procedure for checking whether a Vector has been defined before operating on it?

我来自JavaScript背景,由于其类型松散的系统,我可以简单地做if(isArray(array))。在操作之前是否有任何类似或标准的程序来检查Vector是否已被定义?

1 个解决方案

#1


2  

You want:

你要:

if(myVector != null) {
    // let's work with it!
}

for this. In Java it must have been declared earlier, e.g. Vector myVector.

为了这。在Java中,它必须早先声明,例如矢量myVector。

Also, note that Vector is quite an old collection. ArrayList might be the more suitable fit unless you have particular concurrency considerations in play?

另外,请注意Vector是一个非常古老的集合。除非你有特殊的并发注意事项,否则ArrayList可能更合适?

#1


2  

You want:

你要:

if(myVector != null) {
    // let's work with it!
}

for this. In Java it must have been declared earlier, e.g. Vector myVector.

为了这。在Java中,它必须早先声明,例如矢量myVector。

Also, note that Vector is quite an old collection. ArrayList might be the more suitable fit unless you have particular concurrency considerations in play?

另外,请注意Vector是一个非常古老的集合。除非你有特殊的并发注意事项,否则ArrayList可能更合适?