如何返回通用ArrayList <t> </t>

时间:2012-07-08 11:06:56

标签: java generics arraylist

很抱歉不知道最好的人提出这个问题,但我会在这里解释一下我的问题,我有以下课程:

public class ReadOnlyTable<T extends Model> implements ReadOnly<T> {
...

protected ReadOnlyTable() {
    initTable();
}

protected void reloadDataSource() {
    initTable();
}

...

@Override
public ArrayList<T> findAll() {
    ArrayList<T> result = null;

    try {
        long startTime = System.currentTimeMillis();
        result = datasource.getAllEntries();
        //  // Logger.getLogger().write("FindAllQuery time was " + (System.currentTimeMillis() - startTime) + "ms");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return result;
}

让我们说T是类“Galaxy”,然后我试图循环返回数组中的所有元素,并使用以下代码

    for (Galaxy gal : gDAO.findAll()) {

    }

为什么它会给我一个错误,需要Galaxy,但找到了Object?我做错了什么,我有返回类型ArrayList而不是ArrayList&lt; T&gt;

编辑1:

gDAO就是这样定义的

private static GalaxyDAO gDAO = (GalaxyDAO) DAOFactory.get(GalaxyDAO.class);

DAOFactory看起来像这样

    public static <T> T get(Class clazz) {
    if (!instanceList.containsKey(clazz)) {
        try {

            GenericDAO genericDAO = null;
            Constructor constructor;
            constructor = clazz.getDeclaredConstructor();
            constructor.setAccessible(true);
            genericDAO = (GenericDAO) constructor.newInstance();
            instanceList.put(clazz, genericDAO);

            return (T)genericDAO;
        } catch (NoSuchMethodException ex) {
            DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
        } catch (SecurityException ex) {
            DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
        } catch (InstantiationException ex) {
            DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
        } catch (IllegalAccessException ex) {
            DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
        } catch (InvocationTargetException ex) {
            DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
        }
    }else{
        return (T)instanceList.get(clazz);
    }

    return null;
}

最后

public class GalaxyDAO extends ReadWriteTable<Galaxy> implements GenericDAO {

是的.. ReadWriteTable扩展了ReadOnlyTable

public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable implements ReadWrite<T> {
public ReadWriteTable() {              
    super();
}    

公认解决方案的附录

我的界面出现了错误,导致无法将Type赋予ReadOnlyTable

public interface ReadWrite<T> extends ReadOnly {

而不是

public interface ReadWrite<T> extends ReadOnly<T> {

修复后我也可以改变以下行

public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable<T> implements ReadWrite<T> {

3 个答案:

答案 0 :(得分:1)

gDAO是您班级ReadOnlyTable<T extends Model>的一个实例?什么是类型参数(你是如何声明的)?

必须为Galaxy,否则无效。

更新:现在您发布了更多代码,问题是您必须将类型参数Galaxy传递给ReadOnlyTable<T extends Model>方法所在的类findAll,否则它不会我不知道T是什么,并将返回ArrayList<Object>

这一行是问题所在:

public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable implements ReadWrite<T> 

类ReadOnlyTable还必须接收类型参数:

ReadOnlyTable<T extends Model>

所以该行必须是:

public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable<T extends Model> implements ReadWrite<T> 

答案 1 :(得分:0)

问题是ReadWriteTable声明,您已将其声明为

public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable implements ReadWrite<T>

扩展原始类型ReadOnlyTable而不是参数化ReadOnlyTable<T>。将其更改为

public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable<T> implements ReadWrite<T>

答案 2 :(得分:0)

问题是你将GalaxyDAO声明为行类型,​​它应该被声明为:

public class GalaxyDAO<Galaxy> extends ReadWriteTable<Galaxy> implements GenericDAO

我告诉你,它上面有编译器警告,不要忽略警告,你应该像对错误那样威胁他们!