首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么判断Vector为空

2012-01-03 
如何判断Vector为空?在一个应用中有这样的一行代码:Vectorwcccc.functiona(Stringa)在cccc中publicVecto

如何判断Vector为空?
在一个应用中有这样的一行代码:
Vector   w=cccc.functiona(String   a);


在cccc中
public   Vector   functiona(String   a)
{
Vector   ss=new   Vector();
..........
执行查询操作,得到结果集
ss.add();
...
return     ss
}

问题是:如果cccc.functiona(String   a)得到的结果集是空null
如何判断X为空?
以下无效:
if(!w.isEmpty())
System.out.println((String)w.get(0));
else
System.out.println( "==0 ");

  if(w.size()!=0)
System.out.println((String)w.get(0));
else
System.out.println( "==0 ");
if(w!=null)
System.out.println((String)w.get(0));
else
System.out.println( "==0 ");
当cccc.functiona(String   a)为空时,w.size()==1
都不能输出 "==0 ";


[解决办法]
你所知的X是什么?
cccc.functiona(String a)为空时,w.size()==1?此时w.size()不是应该等于0吗?
[解决办法]
if (w == null) System.out.pritnln( "w is null ");
else if (w.size() == 0) System.out.println( "w is empty ");
else ...
[解决办法]
结果集是null,ss里怎么会有数据??
[解决办法]
lz的代码有问题

if(w!=null) System.out.println((String)w.get(0));
w不为null,就能保证w有元素,这么写肯定是不对的。

你的代码可以写为:
if (w != null && w.size() != 0) //size() == 0与isEmpty()是一回事
System.out.println((String) w.get(0));
else
System.out.println( "null or empty ");

这么写你再测试测试,应该就是对的了。
[解决办法]
你的ss.add();添加了什么?不会是把整个resultset放进去了吧,如果那样的话ss肯定不会为空,(至少有一个元素是你的Resultset,尽管你的Resultset肯能是个null或空集合),除非你add的是单纯的对象,再加上你的ss是new 的,你在主调函数中加if (w ! = null)完全没有意义,这个函数functiona(String a)永远不会返回一个null;

[解决办法]
建议你这样写
Vector w=new Vector();
w=cccc.functiona(String a);
if (v.size()> 0)
{
写你的代码
}
[解决办法]
Up
[解决办法]
回答你的问题
1 方法返回的是NULL
比如
Vector resultVector = someFunction();
if(resultVector==null){
}
2 Vector中的内容为空,即没有内容
Vector resultVector = someFunction();
if(resultVector.size() <=0){
}

[解决办法]
楼主的问题,其实sql语句本身的执行问题。楼主写的sql不是全部的,sql执行后,如果某一行有记录,但是该行中的某一列是“可以为null”的,并且未赋值,那么查询后,该列的返回值就是null;楼主上面的代码其实就是把null这个对象插入了vector,然后又把这个对象打印了出来;显示完全正常。
另,判断vector空与否
就是楼上一位兄弟说的
if(vec == null){ //vector没有初始化

}else if(vec.size() == 0){ //里面没有包含对象

}

热点排行