简体   繁体   English

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

[英]How do I check whether a Vector exists and has already been defined in Java?

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. 我试图在遍历它之前检查一下vector.isEmpty(),但是有时甚至还没有定义向量,因为它是在其他地方的函数中分配的。 Is there a way to check whether vector == Vector before trying to invoke any Vector methods on said vector? 有没有一种方法可以在尝试对上述vector调用任何Vector方法之前检查vector == Vector

I'm coming from a JavaScript background and because of its loosely typed system, I can simply do if (isArray(array)) . 我来自JavaScript背景,由于它的系统类型松散,我可以简单地执行if (isArray(array)) Is there any similar or standard procedure for checking whether a Vector has been defined before operating on it? 是否有任何类似或标准的程序来检查Vector,然后再对其进行操作?

You want: 你要:

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

for this. 为了这。 In Java it must have been declared earlier, eg Vector myVector . 在Java中,必须早先声明它,例如Vector myVector

Also, note that Vector is quite an old collection. 另外,请注意,Vector是一个非常古老的集合。 ArrayList might be the more suitable fit unless you have particular concurrency considerations in play? 除非您有特殊的并发考虑,否则ArrayList可能更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM