简体   繁体   English

java:如何从整体上检查 ArrayList 的类型

[英]java: how to check the type of an ArrayList as a whole

i know how to hard code an algorithm on how to check the types of each object in an arraylist, but is there any other way in checking the type of that ArrayList<> in one go, i mean my application has only three types of arraylist.我知道如何对如何检查数组列表中每个对象的类型的算法进行硬编码,但是有没有其他方法可以一次性检查该 ArrayList<> 的类型,我的意思是我的应用程序只有三种类型的数组列表. Say i have a function that returns an ArrayList, and its definiton is that in a variable o (its an arraylist of object ofcourse) i'll add a person object,假设我有一个返回 ArrayList 的函数,它的定义是在变量 o 中(当然它是对象的数组列表),我将添加一个 person 对象,

o.add(person);

and having added all person data on an arraylist of objects and when a call this function say the name of my function is getData(),并在对象数组列表中添加了所有人员数据,并且在调用此函数时说我的函数名称是 getData(),

ArrayList <Object> obj = getData();

so i know that in that ArrayList that i returned from calling getData() that it is all person object, how do i know the type of that ArrayList?.所以我知道在我从调用 getData() 返回的那个 ArrayList 中,它是 all person 对象,我怎么知道那个 ArrayList 的类型?

Just wanted that function to be more generic in sense, this is for my application though, geniric troughout my application.只是希望该功能在某种意义上更通用,但这适用于我的应用程序,在我的应用程序中通用。

Is there any other way of doing this, i can think of an algorithm but is there any short cut or easy way of doing this?..有没有其他方法可以做到这一点,我可以想到一种算法,但是有没有捷径或简单的方法可以做到这一点?..

There is no such thing as 'the type' of an ArrayList .没有ArrayList “类型”之类的东西。

The class ArrayList stores a list of Object references. ArrayList类存储Object引用列表。 It doesn't know and doesn't care if they are all of some type.它不知道也不关心它们是否都是某种类型。

The Java generic system adds compile-time checking to help you keep track of types. Java 泛型系统添加了编译时检查以帮助您跟踪类型。 So, if you declare ArrayList<Person> , you will get compile errors if you write code that could insert a non- Person into your list.因此,如果您声明ArrayList<Person> ,并且您编写的代码可能会插入非Person到您的列表中,则会出现编译错误。

At runtime, the only way to tell what is in an ArrayList to iterate over all the contained items and check them with instanceof .在运行时,判断ArrayList内容的唯一方法是迭代所有包含的项目并使用instanceof检查它们。

Actually there is a way without casting every item manually(it still is ugly though..)实际上有一种方法可以不用手动投射每个项目(尽管它仍然很丑......)

//start with an arraylist of unknown generic type
ArrayList <Object> obj = getData();

//Make an array from it(basically the same as looping over the list
// and casting it to the real type of the list entries)
Object[] objArr = obj.toArray();

//Check if the array is not empty and if the componentType of the 
//array can hold an instance of the class Person 
if(objArr.length>0 
    && objArr.getClass().getComponentType().isAssignableFrom(Person.class)) {
    // do sth....
}

This should not give any unchecked warnings.这不应该给出任何未经检查的警告。

You could use it like this:你可以这样使用它:

private boolean isArrayOfType(Object[] array,Class<?> aClass) {
    return array.length > 0
            && array.getClass().getComponentType().isAssignableFrom(aClass);
}
Object[] personArr = getData().toArray();
if(isArrayOfType(personArr,Person.class) {
   //Do anything...

}

What won't work is the following:不起作用的是以下内容:

// -> This won't work, sry!
       ArrayList<Person> personArrayList = Arrays.asList((Person[])personArr);

如果列表不为空,则获取第一个元素:

type = this.list.get(0).getClass().getSimpleName();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM