English 中文(简体)
Merge Fal Arrays Java
原标题:Merge null Arrays Java

我书写了一个数据模型,把3个阵列合并为一个类别。 这些阵列的面积(当然)固定,最初定为无效。 我使用这一法典:

public static <T> T[] merge(T[]... arrays) {
    int size = 0;
    for (T[] array : arrays) {
        size += array.length;
    }

    T[] merged = (T[]) Array.newInstance(arrays[0][0].getClass(), size);

    int start = 0;
    for (T[] array : arrays) {
        System.arraycopy(array, 0,
           merged, start, array.length);
        start += array.length;
    }
    return (T[]) merged;
}

这行文不正确:

T[] merged = (T[]) Array.newInstance(arrays[0][0].getClass(), size);

我需要准确描述我使用哪类,但我只找一个没有意义的人!

最佳回答

我将利用藏书图书馆。

public static <T> T[] merge(T[]... arrays) {    
    List<T> list = new LinkedList<T>();
    for(T[] array : arrays) {
        for(T t : array) {
            list.add(t);
        }
    }
    return (T[])(list.toArray());    
}

测试方案:

import java.util.*;
class Merger {
    // break out total length logic. Will probably be inlined, but this seems like
    // functionality that deserves its own method.
    public static <T> int totalLength(T[]... arrays) {
        int length = 0;
        for(T[] arr : arrays) length += arr.length;
        return length;
    }

    public static <T> T[] merge(T[]... arrays) {
        int length = totalLength(arrays);
        if(length == 0) return (T[])new Object[0];
        List<T> list = new ArrayList<T>(length);
        for(T[] array : arrays) {
            for(T t : array) {
                list.add(t);
            }
        }
        return (T[])(list.toArray(arrays[0])); 
    }

    public static void main(String[] args) {
        String[] first = new String[] { "This", "is", "a", "test" };
        String[] second = new String[] { "of", "the", "merger" };
        String[] third = new String[] { "and", "it", "works!" };

        String[] merged = merge(first,second,third);
        for(String s : merged) System.out.println(s);

        // validate that it works for merging all 0 arrays:
        merge();
    }
}
问题回答

You can find the type of the item in the array since there is no item in the array.
null doesn t have a type.

相反,你可以通过以下文字获得阵列的类型:arrays[0].getClass(.getComponentType()。

如下:

String[] strings = new String[3];
Class stringArrayClass = strings.getClass();
Class stringArrayComponentType = stringArrayClass.getComponentType();
System.out.println(stringArrayComponentType);

http://tutorials.jenkov.com/java-reflection/arrays.html#componenttype”rel=“nofollow” http://tutorials.jenkov.com/java-reflection/arrays.html

请注意,您需通过component <>/em>而不是array 类型至Arrays.create





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签