以ArrayList为例

 package com.mydemo;

 import java.util.ArrayList;

 public class CollectionDemo {
public static void main(String[] args) {
// 创建集合对象
//Collection c = new Collection();//Collection是接口,不能实例化
Collection c = new ArrayList();// 多态,父类引用子类对象
//boolean add(E e):添加元素
c.add("hello"); //因为ArrayList允许重复,所以永远可以添加成功
//void clear():清空集合
c.clear() //判断集合中是否包含指定元素:boolean contains(Object o)
System.out.println(c.contains("hello")); //如果集合中有hello,返回true //是否为空:boolean isEmpty()
System.out.println(c.isEmpty()); //删除元素:boolean remove(Object o)
System.out.println(c.remove("hello")); //返回集合元素个数:int size()
System.out.println(c.size()); //将集合转换成一个Object类型的数组:Object[] toArray()
Object[] objs = c.toArray();
for(int i = 0; i<objs.length; i++){
System.out.println(objs[i]);
} System.out.println(al);
} }

Collection中的方法的更多相关文章

  1. Collection中list集合的应用常见的方法

    集合 : 用存放对象的容器(集合)     Collection : 跟接口 : 单列集合          ---> List :有序的 ,元素是可以重复的.          ---> ...

  2. java8在Collection中新增加的方法removeIf

    记得我在以前找工作的经历中,遇到过一个面试官问过我一个很基础的问题.问题是:有一个List中有10个元素,我现在想从中删除3个元素,请问怎么做?我当时也没想,就直接说,List的有自带的remove方 ...

  3. lodash中Collection部分所有方法的总结

    总结一下lodash中Collection的所有的方法,方便对比记忆,也便于使用时候查找. 1.    判断是否符合条件:返回bool: a)  every: 判断每一值是不是都符合条件: 通过 pr ...

  4. Java接口中的方法

    接口中可以含有变量和方法.但是,接口中的变量会被隐式地指定为public static final变量(并且只能是public static final变量,用private修饰会报编译错误),而方法 ...

  5. C#中扩展方法

    什么是扩展方法? 扩展方法顾名思义,就是允许向现有的“类型”添加方法,而无需创建派生类.重新编译或以其他方式修改原来类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 扩 ...

  6. 在含有null值的复杂类的集合(Collection)中取最大值

    在日常编程中,经常遇到要在一组复杂类的集合(Collection)中做比较.取最大值或最小值. 举个最简单的例子,我们要在一个如下结构的集合中选取包含最大值的元素: public class Clas ...

  7. 有关collection中的一些数据结构

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...

  8. Asp.net mvc 中Action 方法的执行(二)

    [toc] 前面介绍了 Action 执行过程中的几个基本的组件,这里介绍 Action 方法的参数绑定. 数据来源 为 Action 方法提供参数绑定的原始数据来源于当前的 Http 请求,可能包含 ...

  9. J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用

    J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用   spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修 ...

随机推荐

  1. A Language Modeling Approach to Predicting Reading Difficulty-paer

    Volume:Proceedings of the Human Language Technology Conference of the North American Chapter of the ...

  2. github/gitee使用办法

    github/gitee只要添加SSH公钥都是可以连接上的 比如把某个文件上传gitee 首先肯定要有权限    否则会一直提醒failed伤心心 接下来说常用语句 git config --list ...

  3. github与github网站push神器

    GitBook.Editor(全英文,无汉化) 链接: http://pan.baidu.com/s/1slIZ5jJ 密码: q9mw source tree (汉化中文) 本地需要安装git客户端 ...

  4. mysql 分组排序前n + 长表转宽表

    MySQL数据库优化的八种方式(经典必看) 建表 CREATE TABLE if not EXISTS `bb` ( `id` int not null primary key auto_increm ...

  5. dos2unix 批量转化文件

    在windows和linux双平台下开发,同时也用git作为同步工具,但前期没有注意,导致很多文件使用windows下的换行符CRLF 参考资料了解dos2unix可以转化格式. 但有个问题,虽然可以 ...

  6. 如何用原生js开发一个Chrome扩展程序

    原文地址:How to Build a Simple Chrome Extension in Vanilla JavaScript 开发一个Chrome扩展程序非常简单,只需要使用原生的js就可以完成 ...

  7. Python 解决: from pip import main ImportError: cannot import name 'main'

    此次报错是因为 pip 升级出的问题: from pip import mainif __name__ == '__main__': sys.exit(main()) 改为: from pip imp ...

  8. [转]linux下编译boost.python

    转自:http://blog.csdn.net/gong_xucheng/article/details/25045407 linux下编译boost.python 最近项目使用c++操作python ...

  9. redis 4.x 安装哨兵模式 sentinel

    1.下载 http://download.redis.io/releases/redis-4.0.11.tar.gz 2.解压 tar zxvf redis-4.0.11.tar.gz 3.安装 cd ...

  10. js延迟

    function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; wh ...