扩展ArrayList和创建新的方法 [英] Extending ArrayList and Creating new methods

查看:157
本文介绍了扩展ArrayList和创建新的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有点问题,抓什么 - 我可能会对此完全错误的。

我想创建延伸的ArrayList,但有几种方法从而增加了功能的类(至少对我开发的程序。)

其中一种方法是一个findById(中间体ID),该搜索每个ArrayList对象对特定ID相匹配。到目前为止,它的工作,但它不会让我做的(项目我:这一点){i.getId(); }

我不明白,为什么?

全部code:

 公共类CustomArrayList<项目>扩展的ArrayList<项目> {    //声明singleton实例
    受保护的静态CustomArrayList实例;    //私有构造
    私人CustomArrayList(){
        // 没做什么
    }    //获取类的实例 - 单
    公共静态CustomArrayList的getInstance(){
        如果(例如== NULL){
            例如=新CustomArrayList();
        }
        返回实例;
    }    公共项目findById(INT ID){
        项目项目= NULL;
        对于(项目我:这一点){
            如果(i.getId()== id)的{
                      //东西
         }
        }
        归还物品;
    }
    公共无效的printList(){
        字符串打印=;
        对于(项目我:这一点){
            打印+ = i.toString()+\\ n;
        }
        的System.out.println(打印);
    }
}


解决方案

修改

 公共类CustomArrayList<项目>扩展的ArrayList<项目> {

 公共类CustomArrayList扩展的ArrayList<项目> {


我怀疑项目是您要在列表中存储类的名称。通过添加<项目> CustomArrayList 你引入的类型参数的这阴影这个类。


的的<项目> 参数,您的code等于

 公共类CustomArrayList< T>扩展的ArrayList< T> {
    // ...
        对于(T I:这一点){i.getId(); }
    // ...
}

这显然并不总是可行的,因为 T 可以指任何类型的。

I'm having a bit of a problem grasping something - I might be going about this completely wrong.

I am trying to create a class which extends ArrayList but has several methods which increase the functionality (at least for the program I am developing.)

One of the methods is a findById(int id), which searches each ArrayList object for a particular id match. So far it's working, but it won't let me do for (Item i : this) { i.getId(); }

I don't understand why?

Full code:

public class CustomArrayList<Item> extends ArrayList<Item> {

    // declare singleton instance
    protected static CustomArrayList instance;

    // private constructor
    private CustomArrayList(){
        // do nothing
    }

    // get instance of class - singleton
    public static CustomArrayList getInstance(){
        if (instance == null){
            instance = new CustomArrayList();
        }
        return instance;
    }

    public Item findById(int id){
        Item item = null;
        for (Item i : this) {
            if (i.getId() == id) {
                      // something
         }
        }
        return item;
    }
    public void printList(){
        String print = "";
        for (Item i : this) {
            print += i.toString() + "\n";
        }
        System.out.println(print);
    }
}

解决方案

Change

public class CustomArrayList<Item> extends ArrayList<Item> {

to

public class CustomArrayList extends ArrayList<Item> {


I suspect Item is the name of the class that you want to store in the list. By adding <Item> after CustomArrayList you're introducing a type parameter which shadows this class.


With the <Item> parameter, your code is equal to

public class CustomArrayList<T> extends ArrayList<T> {
    // ...
        for (T i : this) { i.getId(); }
    // ...
}

which obviously won't always work, as T may refer to any type.

这篇关于扩展ArrayList和创建新的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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