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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12320429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:25:54  来源:igfitidea点击:

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

javaarraylist

提问by lemoncodes

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. 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<> 的类型,我的意思是我的应用程序只有三种类型的数组列表. 假设我有一个返回 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?..

有没有其他方法可以做到这一点,我可以想到一个算法,但是有没有捷径或简单的方法可以做到这一点?..

回答by bmargulies

There is no such thing as 'the type' of an ArrayList.

没有“类型”这样的东西ArrayList

The class ArrayListstores a list of Objectreferences. It doesn't know and doesn't care if they are all of some type.

该类ArrayList存储Object引用列表。它不知道也不关心它们是否都是某种类型。

The Java generic system adds compile-timechecking to help you keep track of types. So, if you declare ArrayList<Person>, you will get compile errors if you write code that could insert a non-Personinto your list.

Java 泛型系统添加了编译时检查以帮助您跟踪类型。因此,如果您声明ArrayList<Person>,并且您编写的代码可能会Person在您的列表中插入一个非,则会出现编译错误。

At runtime, the only way to tell what is in an ArrayListto iterate over all the contained items and check them with instanceof.

在运行时,唯一的方法是告诉 anArrayList中的内容迭代所有包含的项目并使用instanceof.

回答by Leo Gerber

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);

回答by Edward Curzon

If the list is not empty, get the first element:

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

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