java中如何去除字符串数组中的空值
我从数据库中取得一个字符串数组中含有空值,如何做判断,去掉呢。改字符串的形式大概如下:Stringdata[]={"14:22","15:12","13:44","","...
我从数据库中取得一个字符串数组中含有空值,如何做判断,去掉呢。改字符串的形式大概如下:String data[] ={"14:22","15:12","13:44","",""};里面的空值如何去掉呢,如果是用List的集合去复制这个数组的话,代码应该怎么写?
展开
展开全部
String data[] ={"14:22","15:12","13:44","",""};
String st="";
List<String> strlist=Lists.newArrayList();
for (String strings : data) {
strlist.add(strings);
}
for (String strs : strlist) {
if(!strs.equals("")){
st+=strs+",";
}
}
System.out.println(st.substring(0, st.length()-1));
/*
利用for循环比对字符,if(!strs.equals(""))如果数组中的字符非空执行拼接字符串,比对连接后的字符串就是去掉空值的字符串。
*/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-03-30
展开全部
String []data={"14:22","15:12","13:44","",""};
List<String> list=new ArrayList<String>();
for(int i=0;i<data.length&&data.length>0;i++){
if(data[i]==null||"".equals(data[i].trim().toString())){
continue;
}else{
list.add(data[i]);
} }
String []newArray=new String[list.size()];
for(int i=0;i<newArray.length;i++){
newArray[i]=list.get(i);
}
System.out.println("除去空值的新数组就是:"+newArray);
System.out.println("除去空值的List为:"+list);
List<String> list=new ArrayList<String>();
for(int i=0;i<data.length&&data.length>0;i++){
if(data[i]==null||"".equals(data[i].trim().toString())){
continue;
}else{
list.add(data[i]);
} }
String []newArray=new String[list.size()];
for(int i=0;i<newArray.length;i++){
newArray[i]=list.get(i);
}
System.out.println("除去空值的新数组就是:"+newArray);
System.out.println("除去空值的List为:"+list);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) {
String data[] ={"14:22","15:12","13:44","",""};
String st="";
List<String> strlist=Lists.newArrayList();
for (String strings : data) {
strlist.add(strings);
}
for (String strs : strlist) {
if(!strs.equals("")){
st+=strs+",";
}
}
System.out.println(st.substring(0, st.length()-1));
}
运行结果:14:22,15:12,13:44
String data[] ={"14:22","15:12","13:44","",""};
String st="";
List<String> strlist=Lists.newArrayList();
for (String strings : data) {
strlist.add(strings);
}
for (String strs : strlist) {
if(!strs.equals("")){
st+=strs+",";
}
}
System.out.println(st.substring(0, st.length()-1));
}
运行结果:14:22,15:12,13:44
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test1 {
public static void main(String[] args) {
String data[] ={"14:22","15:12","13:44","",""};
List<String> list = new ArrayList<String>();
for(int i=0;i<data.length;i++){
if(!data[i].equals("")){
list.add(data[i]);
System.out.println(list.get(i));
}
}
}
}
public static void main(String[] args) {
String data[] ={"14:22","15:12","13:44","",""};
List<String> list = new ArrayList<String>();
for(int i=0;i<data.length;i++){
if(!data[i].equals("")){
list.add(data[i]);
System.out.println(list.get(i));
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询