简体   繁体   English

将Collection和Iterator接口作为内部类实现

[英]Implementation of Collection and Iterator interface as inner class

I'm trying to implement a Collection interface with Iterator as inner class. 我正在尝试使用Iterator作为内部类实现Collection接口。 An ArrayCollection class that implements collection has a generic array (is it a correct way to say that class members are generic?). 实现集合的ArrayCollection类具有通用数组(说类成员是通用的正确方法吗?)。 a screenshot from IDE IDE的屏幕截图

public class ArrayCollection<T> implements Collection<T> {
private T[] m = (T[])new Object[10];

However when I'm implementing method next() for an Iterator interface I keep getting an Incompatible types error. 但是,当我为Iterator接口实现next()方法时,我总是收到不兼容类型错误。 However if make an ArrayIterator a non-generic class compiler doesn't have problems with array typecasting anymore. 但是,如果将ArrayIterator设为非通用类编译器,数组类型转换将不再存在问题。 an error screenshot from IDE IDE中的错误屏幕截图

    private class ArrayIterator<T> implements Iterator<T> {
    private int cursor = 0;

    @Override
    public boolean hasNext() {
        return this.cursor >= ArrayCollection.this.size();
    }

    @Override
    public T next() {
       return ArrayCollection.this.m[cursor++];
    }
}

So I have few questions: 所以我有几个问题:

  1. How come the compiler can define the T[] m array type if I make an ArrayIterator non-generic? 如果我使ArrayIterator非泛型,编译器如何定义T [] m数组类型?

  2. Is it only the inner classes that implement/extend generic interfaces/classes can be non-generic? 难道只有实现/扩展通用接口/类的内部类可以是非通用的吗?

You named your type variables similarly, ArrayIterator<T> has a different T than ArrayCollection<T> 's T is. 您使用类似的方式命名类型变量, ArrayIterator<T>TArrayCollection<T>T

You can just remove the <T> from ArrayIterator (since it is a non-static inner class) and just have Iterator use the T from the parent class: 您可以从ArrayIterator删除<T> (因为它是一个非静态内部类),而让Iterator使用父类中的T

    private class ArrayIterator implements Iterator<T> {

This will fix the compile issue and your code. 这将解决编译问题和您的代码。

In the second example, you are dealing with two different type variables. 在第二个示例中,您要处理两个不同的类型变量。 The outer class and the inner class each define a variable T , but they are not the same. 外部类和内部类均定义变量T ,但它们并不相同。 There are several ways to solve this, one is to remove the T from the inner class declaration: 有几种解决方法,一种是从内部类声明中删除T:

private class ArrayIterator implements Iterator<T> {

Now you are referencing the outer T only, without introducing a separate inner T 现在,您仅引用外部T,而没有引入单独的内部T

However, I would personally prefer to make the inner class static, in which case you can't use the outer type's variables. 但是,我个人更希望将内部类设为静态,在这种情况下,您将无法使用外部类型的变量。 In this case you'd write 在这种情况下,您会写

private static class ArrayIterator<T> implements Iterator<T> {

If you do this, you need to pass the type variable from the outer type to the inner: 如果这样做,则需要将类型变量从外部类型传递到内部类型:

return new ArrayIterator<T>();

The main difference is that a class that defines a type parameter shadows any existing type parameter of the same name, which leads to that strange error message. 主要区别在于,定义类型参数的类遮盖了任何同名的现有类型参数,从而导致出现奇怪的错误消息。

Finally let me add that it's usually not a good idea to implement a Collection from scratch. 最后,让我补充说,从头开始实现Collection通常不是一个好主意。 Instead, you might want to extend AbstractCollection or AbstractList . 相反,您可能想扩展AbstractCollectionAbstractList This will let you focus on your core algorithm, but provide you with all the boilerplate methods for free. 这将使您专注于核心算法,但是免费为您提供所有样板方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM