扩展 ArrayList 并创建新方法

发布于 2024-11-17 21:37:25 字数 1131 浏览 1 评论 0原文

我在理解某些东西时遇到了一些问题 - 我可能会完全错误地处理这个问题。

我正在尝试创建一个扩展 ArrayList 的类,但有几种方法可以增加功能(至少对于我正在开发的程序而言)。

其中一种方法是 findById(int id),它在每个 ArrayList 对象中搜索特定的 id匹配。到目前为止它正在工作,但它不会让我做 for (Item i : this) { i.getId(); ?

我不明白为什么

完整代码:

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);
    }
}

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-11-24 21:37:25

更改

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

public class CustomArrayList extends ArrayList<Item> {

我怀疑 Item 是您要存储在列表中的类的名称。通过在 CustomArrayList 之后添加 ,您将引入一个隐藏此类的类型参数


使用 参数,您的代码

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

显然不会总是有效,因为 T 可能引用任何类型。

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.

怼怹恏 2024-11-24 21:37:25

什么是getId()?据推测,它是某个类中的方法,但我们不知道哪个类。

如果您实际上有一个名为 Item 的类,其中包含 getId() 方法,这意味着它是一个列表,您只需阻止您的类被通用的。因此,

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

您需要:

public class CustomArrayList extends ArrayList<Item> {

当前在您的类中,Item 不引用名为 Item 的类,它引用名为 Item< 的类型参数 /代码>。

现在就个人而言:

  • 我不会避免创建单例,除非你真的必须这样做。
  • 如果必须的话,我会避免以你所拥有的方式创建它们(这不是线程安全的),
  • 我不会扩展 ArrayListArrayList< > 除非我真的不得不这样做,否则更喜欢组合而不是继承

What is getId()? Presumably it's a method in some class, but we don't know which class.

If you've actually got a class called Item with a getId() method, which this is meant to be a list of, you simply need to stop your class from being generic. So instead of this:

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

you want:

public class CustomArrayList extends ArrayList<Item> {

Currently within your class, Item doesn't refer to a class called Item, it refers to a type parameter called Item.

Now personally:

  • I wouldn't avoid creating singletons unless you really have to
  • If you have to, I'd avoid creating them in the way you have (which isn't thread-safe)
  • I wouldn't extend ArrayList<> unless I really had to, preferring composition over inheritance
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文