java怎么把一个数组每隔四个元素合为一个赋值给另一个数组,数组里装的是字符串

StringBuffer sb = new StringBuffer();
for (int i = 1; i <= answerList.size(); i++) {

sb.append(answerList.get(i - 1));
//System.out.print(sb + " ");
if (i % 4 == 0) {

answer.add(sb.toString());
sb.delete(0, sb.length());
}
}

for(int i=0;i<answer.size();i++){

System.out.println(answer.get(i));

}
这是我写的,嘿嘿,谢谢大家(answer,answerList都是arraylist数组)

import java.util.Arrays;

public class CopyDemo {
    public static void main(String[] args) {
        String[] source = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
                "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        printArray(source);
        String[] out = mergeCopy(source);
        printArray(out);
    }

    public static String[] mergeCopy(String[] source) {
        String[] out = new String[(source.length + 3) / 4];
        for (int i = 0; i < source.length; i++) {
            if (i % 4 == 0) {
                out[i / 4] = source[i];
            } else {
                out[i / 4] += source[i];
            }
        }
        return out;
    }

    private static void printArray(String[] array) {
        System.out.println(Arrays.toString(array));
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-08-03
int k=-1;
for (int i=0;i<stringList.length();i++)
{
    if (i%4==0)
    {
        k++;
        result[k]=new String("");
    }
    result[k]+=stringList[i];
}
result[]是输出数组

第2个回答  2015-08-05
没有直接的这种函数吧。。。自己写几句程序吧。这种字符串拼接的,StringBuffer.append比较方便。
第3个回答  2013-05-27
def arr = [....................];
def arr2 = [];
for(int i = 0;i<arr.length;i+=4){
def tmp = [];

for(int j = i;j<i+4;j++){

tmp.add(arr[i])

}
arr2.add(tmp)

}

arr2就是你想要的