Java,巧妙的方法来替换“if not null”陈述?

发布于 2024-11-30 17:48:12 字数 580 浏览 1 评论 0原文

我有一个充满 longVector

我希望能够始终在 Vector 上调用 getFirstElement(),然后执行操作,例如 addToOtherVector()。我希望能够不用担心原始向量是否确实有返回值。我想我可以通过重写 addToOtherVector() 来做到这一点,如下所示:

//Code to be called when my first vector is not empty
public void addToOtherVector(long s){
    othervector.add(s);
}

//Code to be called when my first vector IS empty
public void addToOtherVector(something???){
    //does nothing
}

但我不确定我需要做什么,因为它不接受 null作为参数?

我这样做的原因是因为我不希望每次尝试检索时都检查向量的大小

I have a Vector full of longs.

I would like to be able to always call getFirstElement() on a Vector and then perform an action, let's say addToOtherVector(). I want to be able to not worry whether or not there is actually a value to return from my original vector. I think I could do it by overriding addToOtherVector() like so:

//Code to be called when my first vector is not empty
public void addToOtherVector(long s){
    othervector.add(s);
}

//Code to be called when my first vector IS empty
public void addToOtherVector(something???){
    //does nothing
}

but I'm not sure what i need to do for the something, as it won't accept null as a parameter?

The reason I am doing this is because I don't wish to have to check the size of the vector each time I try to retrieve

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

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

发布评论

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

评论(2

思念满溢 2024-12-07 17:48:12

只需用基类重写该方法即可。由于 NumberLongInteger 等的基类,只需使用该类:

//Code to be called when my first vector is not empty
public void addToOtherVector(long s){
   othervector.add(s);
}

//Code to be called when my first vector IS empty
public void addToOtherVector(Number s){
   if (s == null) {
       return;
   }
   othervector.add(((Number) s).longValue());
}

Just override the method with a base class. Since Number is the base class to Long, Integer, etc. just use that one :

//Code to be called when my first vector is not empty
public void addToOtherVector(long s){
   othervector.add(s);
}

//Code to be called when my first vector IS empty
public void addToOtherVector(Number s){
   if (s == null) {
       return;
   }
   othervector.add(((Number) s).longValue());
}
ㄟ。诗瑗 2024-12-07 17:48:12
import java.util.Vector;
public class Main {

    static Vector otherVector = new Vector();

    public static void main(String[] args) {

        Vector originalVector = new Vector();
        originalVector.add(1);
        originalVector.add(null);
        originalVector.add(2);

        for (Object obj : originalVector) {
            addToOtherVector(obj);
        }
    }

    public static void addToOtherVector(long s) {
        otherVector.add(s);
        System.out.println("adding " + s + " to vector");
    }

    public static void addToOtherVector(Object obj) {
        System.out.println("not adding " + obj + " to vector");
    }
}
import java.util.Vector;
public class Main {

    static Vector otherVector = new Vector();

    public static void main(String[] args) {

        Vector originalVector = new Vector();
        originalVector.add(1);
        originalVector.add(null);
        originalVector.add(2);

        for (Object obj : originalVector) {
            addToOtherVector(obj);
        }
    }

    public static void addToOtherVector(long s) {
        otherVector.add(s);
        System.out.println("adding " + s + " to vector");
    }

    public static void addToOtherVector(Object obj) {
        System.out.println("not adding " + obj + " to vector");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文