标签:
通过数组或者集合的形式传入泛型数据,然后用特定字符串隔开。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
interface ElemmentJoiner{
public <T> String join(T[] ts,String delimiter);
public <T> String join(Collection<T> ts,String delimiter);
}
class tj implements ElemmentJoiner{
public <T> String join(T[] ts,String delimiter){
String x="";
for (int i = 0; i < ts.length; i++) {
x+=ts[i].toString()+delimiter;
}
int k=delimiter.length();
x=x.substring(0, x.length()-k);
return x;
}
public <T> String join (Collection<T> ts,String delimiter){
String x="";
Iterator<T> iter= ts.iterator();
while(iter.hasNext()){
x+=iter.next().toString()+delimiter;
}
int k=delimiter.length();
x=x.substring(0, x.length()-k);
return x;
}
}
public class collectionTest {
public static void main(String[] args){
tj tj=new tj();
Collection<String> ts=new ArrayList<String>();
ts.add("a");
ts.add("s2");
ts.add("3l");
System.out.println(tj.join(ts, "||"));
System.out.println(tj.join(new Integer[]{1,2,3}, "|"));
}
}
其中集合是通过迭代器的方式遍历。
标签:
原文地址:http://www.cnblogs.com/ychange/p/4482921.html