Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “如何验证特定类型并制作我的列表”;“类型安全”;_Java - Fatal编程技术网

Java “如何验证特定类型并制作我的列表”;“类型安全”;

Java “如何验证特定类型并制作我的列表”;“类型安全”;,java,Java,我正在尝试创建一个自定义列表,以确保只有一种类型的引用可以添加到我的列表中,我正在尝试使其“类型安全” 数组已设置为对象类型,所有内容都继承自对象,因此如何替换/子类型?例如,如果我希望此列表只能获取字符串对象。目前,我的程序可以采用整数类型和字符串类型,如main方法中所示 不幸的是,我不能使用泛型 MyArrayList Main 您可以创建一个自制的双链接列表,方法是创建一个类,该类是列表中的一个元素,包含列表中的上一个元素和下一个元素。这些元素中的每一个都可以有一个参数,即您想要保护的特

我正在尝试创建一个自定义列表,以确保只有一种类型的引用可以添加到我的列表中,我正在尝试使其“类型安全”

数组已设置为对象类型,所有内容都继承自对象,因此如何替换/子类型?例如,如果我希望此列表只能获取字符串对象。目前,我的程序可以采用整数类型和字符串类型,如main方法中所示

不幸的是,我不能使用泛型

MyArrayList

Main


您可以创建一个自制的双链接列表,方法是创建一个类,该类是列表中的一个元素,包含列表中的上一个元素和下一个元素。这些元素中的每一个都可以有一个参数,即您想要保护的特定类型。

如果不使用泛型,您就无法实现此类型的编译时类型安全性。但是,如果您使用非
java.lang.Object
的组件类型动态创建数组,并且

public class MyArrayList implements MyList {

    private Object[] theList;

    public MyArrayList(Class<?> type) {
        this.theList = (Object[]) java.lang.reflect.Array.newInstance(type, 10);
    }

    ...
公共类MyArrayList实现MyList{
私有对象[]列表;
公共MyArrayList(类类型){
this.theList=(Object[])java.lang.reflect.Array.newInstance(类型,10);
}
...
这样,如果传递来创建数组的类是
String.class
,则向其中添加一个整数将导致抛出
java.lang.ArrayStoreException
,并且您可以在
This.theList[n]=object
所在的任何位置尝试/捕获此异常


不过,值得一提的是,正确的方法是在类和调用方类中使用泛型。

如果没有泛型,您必须自己进行所有类型检查,并且无法避免在调用站点进行强制转换

例如,您可以在构造函数中接受所需类型的类对象,并检查所有添加的对象是否都是该类型的实例:

public class MyArrayList implements MyList {

    private Object[] theList;
    private final Class type;

    // I've omitted the generic wildcard here, cause "no generics"
    // Suppress wildcard warnings here if needed
    // never do this in real, post-Java5 code
    public MyArrayList(Class genericType) { 
        //check any preconditions you want on the class
        this.type = genericType;
        this.theList = new Object[0];       
    }

    public void add(Object toAdd) { 
        // omitted code for growing the array here
        this.temp[theList.length] = this.type.cast(toAdd); //throws Exception on type mismatch
    }

}
这使用了在不匹配时抛出ClassCastException的方法。您也可以使用检查tyoe并决定要做什么(例如,抛出不同的异常)



请注意,您的
setType
将是一个坏主意,因为一旦您得到了它,您就不能再确保数组中的值实际上是该类型的。您必须在设置类型时清除数组,这使得该方法毫无用处-此时您可以创建一个新实例。

“我不能使用泛型”.为什么不呢?如果这是一个家庭作业,我会说它是人为的,毫无意义。它教你什么?我必须使用替换/子类型。有什么建议吗?
import com.sun.jdi.IntegerType;

public class Main {

    public static void main(String[] args) {

        Object list = new Object();
        Object myList = new MyArrayList(list);
        MyArrayList newList = new MyArrayList(myList);


        newList.add(2);
        newList.add("Tom");
        newList.add(0.0);

        newList.display();


public class MyArrayList implements MyList {

    private Object[] theList;

    public MyArrayList(Class<?> type) {
        this.theList = (Object[]) java.lang.reflect.Array.newInstance(type, 10);
    }

    ...
public class MyArrayList implements MyList {

    private Object[] theList;
    private final Class type;

    // I've omitted the generic wildcard here, cause "no generics"
    // Suppress wildcard warnings here if needed
    // never do this in real, post-Java5 code
    public MyArrayList(Class genericType) { 
        //check any preconditions you want on the class
        this.type = genericType;
        this.theList = new Object[0];       
    }

    public void add(Object toAdd) { 
        // omitted code for growing the array here
        this.temp[theList.length] = this.type.cast(toAdd); //throws Exception on type mismatch
    }

}