首页 > 其他 > 详细

Collection中的方法

时间:2019-04-21 13:59:58      阅读:95      评论:0      收藏:0      [点我收藏+]

以ArrayList为例

 1 package com.mydemo;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class CollectionDemo {
 6     public static void main(String[] args) {
 7     //        创建集合对象
 8                //Collection c = new Collection();//Collection是接口,不能实例化
 9         Collection c = new ArrayList();// 多态,父类引用子类对象
10               //boolean add(E e):添加元素
11         c.add("hello");    //因为ArrayList允许重复,所以永远可以添加成功
12          //void clear():清空集合
13              c.clear()
14              
15             //判断集合中是否包含指定元素:boolean contains(Object o)
16                System.out.println(c.contains("hello")); //如果集合中有hello,返回true
17         
18             //是否为空:boolean isEmpty()
19             System.out.println(c.isEmpty());
20     
21            //删除元素:boolean remove(Object o)
22            System.out.println(c.remove("hello"));
23 
24            //返回集合元素个数:int size()
25            System.out.println(c.size()); 
26 
27            //将集合转换成一个Object类型的数组:Object[] toArray()
28            Object[] objs = c.toArray();
29            for(int i = 0; i<objs.length; i++){
30                  System.out.println(objs[i]);
31             }
32 
33            
34         System.out.println(al);
35     }
36     
37 }        

 

Collection中的方法

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

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