代码之家  ›  专栏  ›  技术社区  ›  Christian

Java中的合并字符串[]

  •  1
  • Christian  · 技术社区  · 14 年前

    4 回复  |  直到 14 年前
        1
  •  2
  •   xboard    14 年前

    只是另一种可能性:

    public static String[] mergeStrings(String[]...matrix){
        int total = 0;
           for(String[] vector : matrix){
                 total += vector.length;
           }
           String[] resp = new String[total];
    
           for(int i=0; i< matrix.length; i++){
               for(int j=0; j< matrix[i].length; j++){
                    resp[i*matrix.length + j] = matrix[i][j];
               }
           }
           return resp;
    }
    

    您不能使用以下项进行测试:

    public static void main(String[] args) {
            String[] resp =mergeStrings(new String[]{"1","2"}, new String[]{"3", "4", "5"});
            for(String s : resp)
                System.out.println(s);
    }
    
        2
  •  4
  •   bwawok    14 年前

    好吧,您可以创建一个等于所有[]大小的单个[],然后调用System.arraycopy

    http://download.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#arraycopy(java.lang.Object ,int,java.lang.Object,int,int

    把每个人转移到新的大阵营。

    更好的问题是,您的代码是否真的非常关键,以至于您使用array而不是arrayList?ArrayList更容易使用,在大多数情况下应该在[]上使用。

        3
  •  2
  •   BalusC    14 年前

    我建议使用 System#arraycopy() 而是执行平台本机操作(从而获得更好的性能):

    public static String[] concat(String[]... arrays) {
        int length = 0;
        for (String[] array : arrays) {
            length += array.length;
        }
        String[] newArray = new String[length];
        int pos = 0;
        for (String[] array : arrays) {
            System.arraycopy(array, 0, newArray, pos, array.length);
            pos += array.length;
        }
        return newArray;
    }
    

    public static <T> T[] concat(Class<T> type, T[]... arrays) {
        int length = 0;
        for (T[] array : arrays) {
            length += array.length;
        }
        T[] newArray = (T[]) Array.newInstance(type, length);
        int pos = 0;
        for (T[] array : arrays) {
            System.arraycopy(array, 0, newArray, pos, array.length);
            pos += array.length;
        }
        return newArray;
    }
    

    用法示例:

    String[] arr1 = { "foo1", "bar1" };
    String[] arr2 = { "foo2", "bar2", "baz2" };
    String[] arr3 = { "foo3" };
    
    String[] all1 = concat(arr1, arr2, arr3);
    System.out.println(Arrays.toString(all1)); // [foo1, bar1, foo2, bar2, baz2, foo3]
    
    String[] all2 = concat(String.class, arr1, arr2, arr3);
    System.out.println(Arrays.toString(all2)); // [foo1, bar1, foo2, bar2, baz2, foo3]
    
        4
  •  0
  •   willcodejavaforfood    14 年前

    使用java.util.Arrays创建一个集合,然后返回列表:)

        String[] moo1 = {"moo", "moo2"};
        String[] moo2 = {"moo3", "moo4"};
        String[] moo3 = {"moo5", "moo5"};
    
        ArrayList<String> strings = new ArrayList<String>();
        strings.addAll(Arrays.asList(moo1));
        strings.addAll(Arrays.asList(moo2));
        strings.addAll(Arrays.asList(moo3));
        String[] array = strings.toArray(new String[0]);