简体   繁体   English

实例化一个采用泛型集合的对象

[英]Instantiate an object that takes a generic collection

I am learning Java generics and I am trying to adapt some code I developed as an exercise. 我正在学习Java泛型,我正在尝试调整我开发的一些代码作为练习。

In particular, I developed an ArrayVisualizer class that uses Sedgewick's StdDraw library to visualize and animate the behaviour of a dynamic array. 特别是,我开发了一个ArrayVisualizer类,它使用Sedgewick的StdDraw库来可视化和动画化动态数组的行为。 I have my own dynamic array class which supports generics, and I am trying to extend the usage of ArrayVisualizer to anything that is akin to this array. 我有自己的动态数组类,它支持泛型,我试图将ArrayVisualizer的用法扩展到类似于这个数组的任何东西。

In short, my question is: how can you deal with generic types containing other generic types? 简而言之,我的问题是:如何处理包含其他泛型类型的泛型类型?

Here is my thought process: 这是我的思考过程:

  • I started by making this Interface: 我开始制作这个界面:
public interface IterableCollection<Item> {
    void add(Item i);

    Item get(int index) throws IndexOutOfBoundsException; // These can be slow for LinkedList implementations
    void set(int index, Item value) throws IndexOutOfBoundsException;

    int capacity();
    int size(); // Capacity and size can be the same for LinkedList implementations
}
  • Then, I would like my ArrayVisualizer class to take as parameters any class that implements this Interface. 然后,我希望我的ArrayVisualizer类将任何实现此接口的类作为参数。 After some googling, I found that you can't write 经过一些谷歌搜索,我发现你不能写
public class ArrayVisualizer<T implements IterableCollection<?> >

but you have to use extends. 但你必须使用扩展。

However, even if I write that I can't use the T class inside my functions. 但是,即使我写了,我也不能在我的函数中使用T类。

For example, I have a compiler error if I try to generalize this function: 例如,如果我尝试概括此函数,则会出现编译器错误:

public static void draw(T<String> vector) throws Exception 

It is like I have to specify that T is also generic, but I can't find a way to declare it. 这就像我必须指定T也是通用的,但我找不到声明它的方法。 I have tried the following, and none works: 我尝试了以下,但没有一个工作:

public class ArrayVisualizer<T<?> implements IterableCollection<?> >
public class ArrayVisualizer<T<S> implements IterableCollection<S> >

RESOURCES: I have uploaded the working non-generic version of my code as a reference. 资源:我已经上传了我的代码的工作非泛型版本作为参考。

  • DynamicArray.java DynamicArray.java

  • ArrayVisualizer.java (this takes some spaced strings as input) ArrayVisualizer.java (这需要一些间隔的字符串作为输入)

  • InteractiveArrayVisualizer.java (this one doesn't allow to choose the values of the cells but you can fill them by clicking on the next empty spot and you can empty them by clicking on the last open cell. This is meant to use a debug helper for the resize problem of the dynamic array). InteractiveArrayVisualizer.java (这个不允许选择单元格的值,但你可以通过点击下一个空白点填充它们,你可以通过点击最后一个打开的单元格来清空它们。这是为了使用调试助手对于动态数组的调整大小问题)。

Your ArrayVisualizer class must specify both the collection type, and the element type. 您的ArrayVisualizer必须同时指定集合类型和元素类型。 You cannot quantify over T<String> like that in Java. 不能像在Java中那样量化T<String> (Other languages can do it, but not Java.) (其他语言可以做到,但不是Java。)

So, something like.... 所以,像....

class ArrayVisualizer<E, T extends IterableCollection<E>> {
  public void draw(T vector) { ... }
}

(although if I were you, I would try to use the pre-built Iterable or Collection interface rather than my own IterableCollection interface...) (虽然如果我是你,我会尝试使用预先构建的IterableCollection接口而不是我自己的IterableCollection接口...)

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

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