2016-11-13 02:30

# 求这题编程题源码！⋯⋯⋯⋯

⋯⋯⋯⋯求源码⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯

• 写回答
• 好问题 提建议
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• 「已注销」 2016-11-13 15:40
已采纳
``````    @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);

String l = ej.join(list, "~ ");
System.out.println(l);
}
``````
已采纳该答案
评论
解决 无用
打赏 举报
• threenewbee 2016-11-13 05:23
`````` public <T> String join(T[] ts, String delimiter)
{
String r = "";
for (int i = 0; i < ts.length; i++)
{
if (i == 0) r += ts[0].toString(); else r += (delimiter + ts[i].toString());
}
return r;
}
``````
评论
解决 无用
打赏 举报