填充和排序List <?扩展了T&amp; S&GT; [英] Populating and sorting a List&lt;? extends T &amp; S&gt;

查看:140
本文介绍了填充和排序List <?扩展了T&amp; S&GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一组自定义的swing组件,它们实现了像所需的标志或 tabIndex 等各种属性。在尝试填充各个自定义组件的 List 时遇到问题,然后根据 tabIndex Indexed 它实现了一个单一的方法 getIndex()。然后使用我的 IndexedComparator

我的类进行排序:



索引:

  public interface索引{
public int getIndex();

IndexedComparator:

  public class IndexedComparator实现比较器<索引> {

@Override
public int compare(Indexed o1,Indexed o2){
return o1.getIndex() - o2.getIndex();
}

}

WWTextField:

  public class WWTextField extends JTextField implements Indexed,FocusListener {
private boolean required;
private int tabIndex;

...

@Override
public int getIndex(){
return tabIndex;


$ / code $ / pre

NewJFrame:

  public class NewJFrame extends JFrame {
List<扩展了Component&索引> list = new ArrayList<>();
IndexedFocusTraversalPolicy policy = new IndexedFocusTraversalPolicy();

public NewJFrame(){
initComponents();

list.add(wWTextField1);
list.add(wWTextField2);
list.add(wWTextField3);
list.add(wWTextField4);
list.add(wWTextField5);
list.add(wWFormatedTextField1);
list.add(wWFormatedTextField2);

Collections.sort(list);
policy.populateComponents(list);
this.setFocusTraversalPolicy(policy);


$ / code $ / pre
$ b $编辑:我忘了发布一个实际的问题。为什么我的实现不是

  List< ;?扩展了Component&索引> list = new ArrayList<>(); 

工作?当我尝试编译时,我得到这些错误:

  NewJFrame.java:22:error:>预计
NewJFrame.java:22:错误:';'预计
NewJFrame.java:22:错误:非法开始类型


解决方案

只有在声明类型参数的情况下才允许约束(即, class C< T extends I& J> ; {} 接口E < T extends I& amp ; J> void f(){} )。



因此,您可以简化为给出交集类型名称。这个非功能特性是我关于Java的许多事情之一。



我猜想 NewJFrame 可能是通用的在 T中扩展Component&索引 list 可能有类型 List< T> 。在某些情况下,类型推断可能会让客户端代码避免指定特定的类型。

I'm making a set of custom swing components that implement various properties like a required flag or tabIndex. I'm having problems when trying to populate a List of the various custom components, and then sorting the list based on the tabIndex of each component.

How I'm trying to do this is by having my components implement an interface called Indexed which implements a single method getIndex(). Then sorting using my IndexedComparator.

My classes:

Indexed:

public interface Indexed {
      public int getIndex();
}

IndexedComparator:

public class IndexedComparator implements Comparator<Indexed> {

    @Override
    public int compare(Indexed o1, Indexed o2) {
        return o1.getIndex() - o2.getIndex();
    }

}

WWTextField:

public class WWTextField extends JTextField implements Indexed, FocusListener {
    private boolean required;
    private int tabIndex;

   ...

    @Override
    public int getIndex() {
        return tabIndex;
    }
}

NewJFrame:

public class NewJFrame extends JFrame {
    List<? extends Component & Indexed> list = new ArrayList<>();
    IndexedFocusTraversalPolicy policy = new IndexedFocusTraversalPolicy();

    public NewJFrame() {
        initComponents();

        list.add(wWTextField1);
        list.add(wWTextField2);
        list.add(wWTextField3);
        list.add(wWTextField4);
        list.add(wWTextField5);
        list.add(wWFormatedTextField1);
        list.add(wWFormatedTextField2);

        Collections.sort(list);
        policy.populateComponents(list);
        this.setFocusTraversalPolicy(policy);
    }
}

Edit: I forgot to post an actual question. Why doesn't my implementation of

List<? extends Component & Indexed> list = new ArrayList<>();

Work? When I try to compile I get these errors:

NewJFrame.java:22: error: > expected  
NewJFrame.java:22: error: ';' expected  
NewJFrame.java:22: error: illegal start of type  

解决方案

Constraints are only allowed where type parameters are declared (i.e., class C< T extends I & J > {}, interface E< T extends I & J > {}, < T extends I & J > void f() {}).

You are thus reduced to giving intersection types names. This non-feature is one of the many things that bug me about Java.

I suppose NewJFrame could be generic in T extends Component & Indexed, and list could have type List< T >. In some cases type inference could let client code avoid specifying the specific type.

这篇关于填充和排序List <?扩展了T&amp; S&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆