java连接数组中的元素

时间:2013-02-14 11:50:47

标签: java arrays join

假设我有一个int数组:

intarray = [2,3,5,7,8,8,9,3...]

如何加入前5个元素或其他元素?

例如,我会有数字235783578857889 ......?

我正在尝试这样做因为我需要将数字相互比较

顺便说一下,我仍然在寻找能够保存所有关于java语言的文档的优秀资源

4 个答案:

答案 0 :(得分:2)

pseudocode:

    int frstNumber = 0;
    for i = 0 to 4
         firstNumber *= 10;
         firstNumber += array[i];

    nextNumber = firstNumber
    for i = 5 to end of array
         nextNumber = (nextNumber mod 10000) * 10 + array[i]

答案 1 :(得分:2)

public static void main(String[] args) {
    int[] intarray = new int[] { 2, 3, 5, 7, 8, 8, 9, 3 };

    for (int j = 0; j < intarray.length - 4; j++) {
        String s = "";
        for (int i = j; i < j + 5; i++) {
            s = s + String.valueOf(intarray[i]);
        }
        int value = Integer.parseInt(s);
        System.out.println(value);
    }
}

输出:

23578
35788
57889
78893

答案 2 :(得分:0)

如果您想使用图书馆并在更高级别工作,请尝试Guava

    int[] ary = {7,4,1,2,5,8,9,3};
    Iterable<int[]> first5 = Iterables.limit(Lists.newArrayList(ary), 5);
    String joined = Joiner.on("").join(first5);

请参阅Iterables.limit()

答案 3 :(得分:0)

StringUtils.join([1, 2, 3], null) = "123"

from apache commons-lang