确定集合或数组中的对象类型

Ris*_*shi 10 java arrays collections

假设我有一个数组int [] []或数组char [] []或ArrayList.java中有没有办法知道数组的基类类型.例如:

int[][] gives output as int.
char[][] gives output as char.
ArrayList<Integer> gives output Integer.
ArrayList<Point> gives Point. (It should also work for a custom type)
Run Code Online (Sandbox Code Playgroud)

这可以用Java完成吗?

tom*_*tom 15

数组(例如int[][])

您可以使用getComponentType()获取数组的组件类型:

(new int[10][10]).getClass().getComponentType().getComponentType(); // int
Run Code Online (Sandbox Code Playgroud)

对于任意深度的数组,使用循环:

Object array = new int[10][][][];
Class<?> type = array.getClass();
while (type.isArray())
{
    type = type.getComponentType();
}
assert type == Integer.TYPE;
Run Code Online (Sandbox Code Playgroud)

通用类型(例如ArrayList<Integer>)

无法获取类型参数.Java使用类型擦除,因此信息在运行时丢失.

您可以根据元素的类型猜测声明的集合类型:

import java.util.*;

public class CollectionTypeGuesser
{
    static Set<Class<?>> supers(Class<?> c)
    {
        if (c == null) return new HashSet<Class<?>>();

        Set<Class<?>> s = supers(c.getSuperclass());
        s.add(c);
        return s;
    }

    static Class<?> lowestCommonSuper(Class<?> a, Class<?> b)
    {
        Set<Class<?>> aSupers = supers(a);
        while (!aSupers.contains(b))
        {
            b = b.getSuperclass();
        }
        return b;
    }

    static Class<?> guessElementType(Collection<?> collection)
    {
        Class<?> guess = null;
        for (Object o : collection)
        {
            if (o != null)
            {
                if (guess == null)
                {
                    guess = o.getClass();
                }
                else if (guess != o.getClass())
                {
                    guess = lowestCommonSuper(guess, o.getClass());
                }
            }
        }
        return guess;
    }

    static class C1 { }
    static class C2 extends C1 { }
    static class C3A extends C2 { }
    static class C3B extends C2 { }

    public static void main(String[] args)
    {
        ArrayList<Integer> listOfInt = new ArrayList<Integer>();
        System.out.println(guessElementType(listOfInt)); // null
        listOfInt.add(42);
        System.out.println(guessElementType(listOfInt)); // Integer

        ArrayList<C1> listOfC1 = new ArrayList<C1>();
        listOfC1.add(new C3A());
        System.out.println(guessElementType(listOfC1)); // C3A
        listOfC1.add(new C3B());
        System.out.println(guessElementType(listOfC1)); // C2
        listOfC1.add(new C1());
        System.out.println(guessElementType(listOfC1)); // C1
    }
}
Run Code Online (Sandbox Code Playgroud)


Cha*_*ase 6

你可以使用这些类getComponentType().例如.

public static final Class<?> getBaseType(Object obj) {
    Class<?> type = obj.getClass();
    while (type.isArray()) {
        type = type.getComponentType();
    }
    return type;
}
Run Code Online (Sandbox Code Playgroud)

type 那就是基本类型.

如果objdouble[][][][][][]或只是double[],或其他东西,这将工作.

至于仿制药,那些东西在<>.发生类型擦除,这意味着您无法确定它们ArrayList自身的类型.