ArrayList的contains方法注意

      public static void main(String args[]){
            Collection coll = new ArrayList();
            coll.add(new String("Tom"));
            System.out.println(coll.contains(new String("Tom")));
      }

该代码输出为 true,原因在于 ArrayList 中的 contains 方法是通过 equals 方法比较的,而不是 ==

posted @ 2020-08-27 16:15  ch-一念之间  阅读(183)  评论(0编辑  收藏  举报