首页 > 其他 > 详细

借助查找元素索引来判断元素是否存在

时间:2019-04-24 21:23:54      阅读:107      评论:0      收藏:0      [点我收藏+]
public static void main(String[] args) {
        List list = new ArrayList();
        list.add("java");
        list.add("b");
        list.add("c");
        list.add("d");
        
//        int index = index(list,"e");
//        boolean isset = isSet(list,"java");
        boolean isset = list.contains("b");
        System.out.println(isset);
    }
    public static int index(List list, Object other){
        for(int i = 0;i<list.size();i++){
            Object obj = list.get(i);
            if(obj.equals(other)){
                return i;
            }
        }
        return -1;
    }
    public static boolean isSet(List list, Object other){
        int index = index(list,other);
        if(index>=0){
            return true;
        }else{
            return false;
        }
    }

 

借助查找元素索引来判断元素是否存在

原文:https://www.cnblogs.com/yifengs/p/10764979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!