Day07_35_Colection下的方法

Collection 下的方法

 *  **Collection 集合的方法应用**


```
         package com.shige.Collection;

         import java.util.ArrayList;
         import java.util.Collection;

         public class CollectionMethod01 {

                     public static void main(String[] args) {


                            // 使用多态创建集合对象
                            Collection collection=new ArrayList();  // 父类引用指向子类对象
                            Collection collection1=new ArrayList();

                            // 1. boolean  add(Object element)   向集合中添加元素
                            // Connection 只能单个存储元素,而且只能存储引用类型数据
                            // JDK1.5之后可以直接添加int类型数据,因为会自动装箱成为Integer

                            //添加数据对象
                            collection.add(new Integer(100));
                            collection.add(1);     //JDK1.5之后自动装箱

                            //创建自建类对象 
                            Customer customer=new Customer("施歌",18);
                            Customer customer1=new Customer("李飞",20);
                            Customer customer2=new Customer("王妍",21);

                            //添加对象 add(Object element)   
                            collection.add(customer);
                            collection.add(customer1);
                            collection.add(customer2);


                            //2.int size(); 获取集合中元素的个数, 集合名.size();

                            System.out.println(collection.size()); // 3

                            //3.  void clear(); 清空集合中的元素,  集合名.clear();

                           
                             System.out.println(collection.size()); // 0


                            //4.boolean isEmpty(); 判断集合是否为空, 集合名.isEmpty();

                             if(collection.isEmpty()){
                                 System.out.println("集合为空");
                             }else{
                                 System.out.println("集合不为空");
                             }

                             // 5. Object[] toArray();  将集合转换为数组

                             Object[] objs=collection.toArray();

                                 // 遍历转换后的对象数组
                                 for(int i=0;i<objs.length;i++){
                                     System.out.println(objs[i]);
                                 }


                             //6. 移除集合中的某个元素     集合名.remove();

                                  collection.remove(customer1);


                            //7. 判断集合中是否含有某个元素   集合名.contains();

                                 if(collection.contains(customer1)){
                                     System.out.println("含有此元素");

                                 }else{
                                     System.out.println("找不到此元素");
                                 }


                           //8.将一个集合中的元素全部添加到另一个集合中,集合名.addAll();

                               collection1.addAll(collection);

                                 //将集合转换为数组
                               Object[] objects=collection1.toArray();

                               //遍历转换后的数组
                               for(int i=0;i<objects.length;i++){
                                   System.out.println("collection1  "+objects[i]);
                               }

                }

         }
posted @ 2021-03-31 19:18  失昼  阅读(32)  评论(0)    收藏  举报