我如何找出键入每个对象在ArrayList的<对象&gt ;? [英] How do I find out what type each object is in a ArrayList<Object>?

查看:118
本文介绍了我如何找出键入每个对象在ArrayList的<对象&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定了从数据库导入不同的元素,组成的字符串,数字,双打和整数的ArrayList的。有没有办法使用反射型技术,找出每类元素中包含的数据有什么办法?

I have a ArrayList made up of different elements imported from a db, made up of strings, numbers, doubles and ints. Is there a way to use a reflection type technique to find out what each type of data each element holds?

供参考:,原因是有许多类型的数据是,这是一个片的java $ C $的c被写入具有不同的DB的实施

FYI: The reason that there is so many types of data is that this is a piece of java code being written to be implemented with different DB's.

推荐答案

在C#:<​​BR/>修正建议从迈克

ArrayList list = ...;
// List<object> list = ...;
foreach (object o in list) {
    if (o is int) {
        HandleInt((int)o);
    }
    else if (o is string) {
        HandleString((string)o);
    }
    ...
}

在Java的:

ArrayList<Object> list = ...;
for (Object o : list) {
    if (o.getClass().equals(Integer.class)) {
        handleInt((int)o);
    }
    else if (o.getClass().equals(String.class)) {
        handleString((String)o);
    }
    ...
}

这篇关于我如何找出键入每个对象在ArrayList的&LT;对象&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆