Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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_Eclipse_Type Erasure_Bidirectional_Bimap - Fatal编程技术网

Java “如何走动”;方法的擦除与另一种方法相同;在编写双向映射时

Java “如何走动”;方法的擦除与另一种方法相同;在编写双向映射时,java,eclipse,type-erasure,bidirectional,bimap,Java,Eclipse,Type Erasure,Bidirectional,Bimap,我目前正在尝试编写一个双向映射,因为(据我所知)Java不提供双向映射。我的代码如下 private static final class ColourCharTwoWayMap<E,F> { private ArrayList<E> eArrayList; private ArrayList<F> fArrayList; private ColourCharTwoWayMap() { eArrayList = ne

我目前正在尝试编写一个双向映射,因为(据我所知)Java不提供双向映射。我的代码如下

private static final class ColourCharTwoWayMap<E,F> {

    private ArrayList<E> eArrayList;
    private ArrayList<F> fArrayList;

    private ColourCharTwoWayMap() {
        eArrayList = new ArrayList<E>();
        fArrayList = new ArrayList<F>();
    }

    public void put(E colour, F ch) {
        eArrayList.add(colour);
        fArrayList.add(ch);
    }

    public F get(E colour) throws ArrayIndexOutOfBoundsException {
        return fArrayList.get(eArrayList.indexOf(colour));
    }

    public E get(F ch) throws ArrayIndexOutOfBoundsException {
        return eArrayList.get(fArrayList.indexOf(ch));
    }
}
private静态最终类colorChartWoWayMap{
私人ArrayList耳环列表;
私人ArrayList fArrayList;
专用彩色图表WOWayMap(){
ArrayList=新的ArrayList();
fArrayList=新的ArrayList();
}
公众认股权证(E色、F色){
耳环列表。添加(颜色);
fArrayList.add(ch);
}
public F get(E color)将数组索引抛出边界异常{
返回fArrayList.get(arraylist.indexOf(color));
}
public E get(F ch)将ArrayIndexOutof BoundsException抛出{
返回arraylist.get(fArrayList.indexOf(ch));
}
}
Eclipse给我的错误是“擦除方法get(E)与类型SaveManager.colorChartWoWayMap中的另一个方法相同”。通过谷歌搜索,我了解到Java不喜欢做同样事情的泛型方法,这与重写和Java不知道使用什么方法有关。这对我来说有点过头了


做上面我想做的事,有什么更好的方法?(即有一个方法,该方法接受类型为E的对象并返回类型为F的对象,反之亦然)。

由于您已经提到的类型擦除效应,您的两个get函数具有相同的参数类型-java.lang.object。这显然是不允许的,因为函数名也是相同的,编译器会发出错误


解决方案很简单,将名称更改为getE和getF

更好的方法是为两个getter指定不同的名称。

如果这两个值中的任何一个都应该有一个特定的父类,即看起来像Color和Char是您的值,您可以绕过类型擦除,但允许对Color和Char进行子分类,通过说
E扩展颜色
F扩展字符
。这使它们更加“具体”,编译器能够区分两者。但是,如果颜色或字符来自同一父类,即对象或字符串,则需要实现不同的方法签名


IE
public F getchbycolor(E color)
public E getcolorbychar(F char)

只是一个附带问题,你为什么不在内部使用地图?别忘了接受答案,这有助于网站了解哪一个对你帮助更大