java 如何将list<123l,456l, 789l>里的元素,组装成一条字符串,每个元素用逗号分隔?
list<123l,456l, 789l>
List<String>是这种类型吗,
List.add("1231");
List.add("4561");
List.add("7891");
list参数这样装的吗
import java.util.ArrayList;
import java.util.List;
public class T2 {
/**
* @param args
*/
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1231");
list.add("4561");
list.add("7891");
sqlIn(list);
}
public static void sqlIn(List<String> list){
StringBuilder sb = new StringBuilder();
if(list != null && list.size()>0){
for(int i=0,len=list.size();i<len;i++){
sb.append("'"+ list.get(i) + "'");
if(i < len-1){
sb.append(",");
}
}
}
System.out.println(sb.toString());
//'1231','4561','7891'
}
}
list 元素是Long类型的,里面是“1520”,“1321”“132”这样的数字?
import java.util.ArrayList;
import java.util.List;
public class T2 {
/**
* @param args
*/
public static void main(String[] args) {
List<Long> list = new ArrayList<Long>();
list.add(1231l);
list.add(4561l);
list.add(7891l);
sqlIn(list);
}
public static void sqlIn(List<Long> list){
StringBuilder sb = new StringBuilder();
if(list != null && list.size()>0){
for(int i=0,len=list.size();i<len;i++){
sb.append("'"+ list.get(i) + "'");
if(i < len-1){
sb.append(",");
}
}
}
System.out.println(sb.toString());
//'1231','4561','7891'
}
}
package com;
import java.util.Arrays;
import java.util.List;
public class Kyo
{
public static void main(String[] args)
{
List<String> list = Arrays.asList("1231","4561","7891");
String sql = "select * from table where id in ";
sql += list.toString().replaceAll("\\[", "(").replaceAll("\\]", ")");
System.out.println(sql);
}
}
打印了一下结果是:
str=null(grp.budget.bs.model.AgencyRation@db13b1, grp.budget.bs.model.AgencyRation@e9ecc8),这是不是说LIST《》内的元素是对象,不是long字符?前面的null是什么意思?
你的是对象model模型封装的,你的模型层需要一个重写的toString方法,返回该类有意义的内容
然后你再toString你的list就可以得到准确的值了
2015-10-28