⋯⋯⋯⋯求源碼⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯
@Override
public <T> String join(T[] ts, String delimiter) {
if (delimiter == null) {
delimiter = "";
}
if (ts.length < 1) {
return delimiter;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ts.length - 1; i++) {
sb.append(ts[i].toString()).append(delimiter);
}
sb.append(ts[ts.length - 1]);
return sb.toString();
}
@Override
public <T> String join(Collection<T> ts, String delimiter) {
if (delimiter == null) {
delimiter = "";
}
if (ts.size() < 1) {
return delimiter;
}
StringBuilder sb = new StringBuilder();
Iterator<T> iter = ts.iterator();
while (iter.hasNext()) {
sb.append(iter.next().toString()).append(delimiter);
}
sb.delete(sb.length() - delimiter.length(), sb.length());
return sb.toString();
}
public static void main(String[] args) {
ElementJoiner ej = new ElementJoinerImpl();
String arr = ej.join(new Integer[]{1, 2, 3}, "/");
System.out.println(arr);
List<Integer> list = new LinkedList<>();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
String l = ej.join(list, "~ ");
System.out.println(l);
}