如何将旧式枚举转换为“通用”枚举?我想确保枚举中每个元素的类型正确。我想确保代码中没有运行时转换错误,尤其是当我没有抓住它们时。

这是我的示例代码。

import java.util.Enumeration;
import java.util.Vector;

public class TestEnumerationCast {

public static void main(String[] args) {
    new TestEnumerationCast();
}

{
    Vector stringVector = new Vector();
    stringVector.add("A");
    stringVector.add("B");
    stringVector.add("C");
    stringVector.add(new Integer(1));

    Enumeration<String> enumerationString = castEnumeration(stringVector.elements());
    while (enumerationString.hasMoreElements()) {
        String stringToPrint = enumerationString.nextElement();
        System.out.println(stringToPrint);
    }

}

private <T> Enumeration<T> castEnumeration(Enumeration<?> elements) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add((T) elements.nextElement());
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}


}

我认为方法castEnumeration会将旧代码转换为任何类型的通用代码。简单地说,我遍历每个元素,尝试将其强制转换为(T)。如果失败,则抛出运行时异常,但仅跳过该元素。然后,我只有一个枚举类型。但是,将类型添加到向量的行未捕获整数。在最后一个元素(整数)的字符串转换中,我仍然遇到运行时异常。

我知道我可以直接转换为泛型类型,忽略错误等。所有都是有效的方法。但是我想确保当我不寻找它时,不会出现运行时异常。谢谢

最佳答案

啊,找到了!您必须指定要在转换中使用的类作为方法参数,然后使用.cast(obj)方法。

更改通话

private <T> Enumeration<T> castEnumeration(Enumeration<?> elements) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add((T) elements.nextElement());
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}




private <T> Enumeration<T> castEnumeration(Enumeration<?> elements, Class<T> tClass) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add(tClass.cast(elements.nextElement()));
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}


并且还将方法的调用从

Enumeration<String> enumerationString = castEnumeration(stringVector.elements());




Enumeration<String> enumerationString = castEnumeration(stringVector.elements(), String.class);


因此,总的来说,代码现在看起来像。

import java.util.Enumeration;
import java.util.Vector;

public class TestEnumerationCast {

public static void main(String[] args) {
    new TestEnumerationCast();
}

{
    Vector stringVector = new Vector();
    stringVector.add("A");
    stringVector.add("B");
    stringVector.add("C");
    stringVector.add(new Integer(1));

    Enumeration<String> enumerationString2 = castEnumeration(stringVector.elements(), String.class);
    while (enumerationString2.hasMoreElements()) {
        String stringToPrint = enumerationString2.nextElement();
        System.out.println(stringToPrint);
    }

}

private <T> Enumeration<T> castEnumeration(Enumeration<?> elements, Class<T> tClass) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add(tClass.cast(elements.nextElement()));
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}

}

关于java - 从遗留代码到泛型的Java泛型转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23613579/

10-10 17:25
查看更多