collection及相关方法
发布时间:2021-07-06 05:38:46 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:import java.util.ArrayList; import java.util.Collection; /* ?* collection接口中的方法,是集合中所有实现类必须拥有的方法 ?* 使用collection接口的实现类,程序的演示 ?* ArrayList implements List ?* List extends Collection ?* 方法的执行,都是实
import java.util.ArrayList;
import java.util.Collection; /* ?* collection接口中的方法,是集合中所有实现类必须拥有的方法 ?* 使用collection接口的实现类,程序的演示 ?* ArrayList implements List ?* List extends Collection ?* 方法的执行,都是实现类的重写 ?*/ public class CollectionDemo { ?? ?public static void main(String[] args) { ?? ??? ?function(); ?? ??? ? ?? ??? ?function_1(); ?? ??? ? ?? ??? ?function_2(); ?? ??? ? ?? ??? ?function_3(); ?? ?} ?? ? ?? ?public static void function(){ ?? ??? ?//接口多态的方式调用 ?? ??? ?Collection<String> coll=new ArrayList<String>(); ?? ??? ?coll.add("abc"); ?? ??? ?coll.add("bcd"); ?? ??? ?System.out.println(coll); ?? ??? ?//清空集合中的所有元素 ?? ??? ?coll.clear(); ?? ??? ?System.out.println(coll); ?? ?} ?? ? ?? ?public static void function_1(){ ?? ??? ?Collection<String> coll=new ArrayList<String>(); ?? ??? ?coll.add("abc"); ?? ??? ?coll.add("bcde"); ?? ??? ?coll.add("cdefg"); ?? ??? ? ?? ??? ?System.out.println("==============="); ?? ??? ?boolean b= coll.contains("bcde"); ?? ??? ?System.out.println(b); ?? ?} ?? ? ?? ?public static void function_2(){ ?? ??? ?Collection<String> coll=new ArrayList<String>(); ?? ??? ?coll.add("abc"); ?? ??? ?coll.add("bcde"); ?? ??? ?coll.add("cdefg"); ?? ??? ?coll.add("123"); ?? ??? ? ?? ??? ?System.out.println("==============="); ?? ??? ?//将集合转成数组 ?? ??? ?Object[] objs= coll.toArray(); ?? ??? ?for(int i=0;i<objs.length;i++) ?? ??? ??? ?System.out.println(objs[i]); ?? ?} ?? ? ?? ?public static void function_3(){ ?? ??? ?Collection<String> coll=new ArrayList<String>(); ?? ??? ?coll.add("abc"); ?? ??? ?coll.add("bcde"); ?? ??? ?coll.add("cdefg"); ?? ??? ?coll.add("123"); ?? ??? ? ?? ??? ?System.out.println("==============="); ?? ??? ?System.out.println(coll); ?? ??? ?boolean b= coll.remove("123"); ?? ??? ?System.out.println(b); ?? ??? ?System.out.println(coll); ?? ?} } (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |