栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Collection集合的常用方法和遍历

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Collection集合的常用方法和遍历

练习程序

package _4;

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

//Collection集合常用方法
//boolean add(E e)添加
//boolean remove(Object o)移除
//void clear()清空
//boolean contains(Object 0)判断元素是否存在
//boolean isEmpty()判断是否为空
//int size()元素个数
public class _222 {
    public static void main(String[] args) {

        Collection c = new ArrayList();


        //boolean add(E e)添加
        System.out.println(c.add("hello"));
        System.out.println(c.add("world"));
        System.out.println(c.add("world"));


        //boolean remove(Object o)移除
        System.out.println(c.remove("world"));
        System.out.println(c.remove("qweer"));


        //void clear()清空
 //     c.clear();


        //boolean contains(Object 0)判断元素是否存在
        System.out.println(c.contains("world"));
        System.out.println(c.contains("sdrev"));

        //boolean isEmpty()判断是否为空
        System.out.println(c.isEmpty());

        //int size()元素个数
        System.out.println(c.size());




        //输出集合
        System.out.println(c);




    }

}

 遍历

 

练习程序

package _4;

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

//Collection遍历
public class _223 {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");
        c.add("world");
        c.add("java");

        Iterator it = c.iterator();
        
        //E next()返回迭代中的下一个元素
        


        //boolean hasNext()如果迭代具有更多元素,则返回true
        


        //用while改进
        while (it.hasNext()){
            //System.out.println(it.next());
            String s=it.next();
            System.out.println(s);
        }



    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778340.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号