Java 怎么从指定索引开始将数组连接到字符串

2019年7月2日21:41:56Java 怎么从指定索引开始将数组连接到字符串已关闭评论 369 761字阅读2分32秒

1.简介

Java 怎么从指定索引开始将数组连接到字符串?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        String[] array = new String[] { "1", "abc", "level", null,
                "tah1986.com", "asdf 123" };
        String joint = "tah1986.com";
        System.out.println(join(array, joint));
    }

    public static String join(String[] array, int startIndex, String joint) {
        String result = "";
        for (int i = startIndex; i < array.length; i++) {
            result += array[i];
            if (i < array.length - 1) {
                result += joint;
            }
        }
        return result;
    }

    public static String join(String[] array, String joint) {
        return join(array, 0, joint);
    }

    public static String join(Collection<?> collection, String joint) {
        String result = "";
        int i = 0;
        int size = collection.size();
        for (Object each : collection) {
            result += each.toString();
            if (i < size - 1) {
                result += joint;
            }
        }
        return result;
    }
}

 
编程十万个怎么办