Collection集合

    技术2024-08-09  77

    Collection集合

    Collection集合概述

    是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素JDK不提供出接口的任何直接实现,它提供更具体的子接口(如Set和LIst)实现

    创建Collection集合的对象

    多态的方式 具体的实现类ArrayList import java.util.ArrayList; import java.util.Collection; /* * 创建Collection集合的的对象 * 多态的方式 * ArrayList() */ public class Collection1 { public static void main(String[] args) { //创建Collection集合的对象 Collection<String> c= new ArrayList<String>(); //添加元素:boolean add(E e) c.add("hello"); c.add("world"); c.add("java"); //输出集合对象 System.out.println(c); } }

    Collection集合常见方法

    package com.fist1; import java.util.ArrayList; public class ArrayLiatDemo2 { public static void main(String[] args) { // 创建集合 ArrayList<String> array = new ArrayList<>(); // 添加元素 array.add("hello"); array.add("java"); array.add("world"); // 一、public boolean remove(Object o) ,删除指定的元素,返回删除是否成功 // System.out.println(array.remove("world")); // System.out.println(array.remove("javaee")); // 二、 public E remove(int index) 删除指定索引的元素,返回被删除的元素 // IndexOutOfBoundsExceptio 索引越界 // System.out.println(array.remove(4)); // 三、public E set (int index,E element)| 修改指定的元素,返回被修改的元素 // System.out.println(array.set(1, "javaee")); // IndexOutOfBoundsException索引越界 // System.out.println(array.set(3, "javaee")); // public E get (int index) 返回指定索引的元素 // System.out.println(array.get(0)); // System.out.println(array.get(1)); // System.out.println(array.get(2)); // System.out.println(array.get(3)); // IndexOutOfBoundsException索引越界问题 // public int size()|放回集合中的元素的个数 System.out.println(array.size()); // 输出集合 System.out.println("array:" + array); } }

    Collection集合的遍历

    lterator:迭代器,集合的专用遍历方式lteratoriterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖集合而存在的lterator中的常用方法E next():返回迭代中的下一个元素blooean has Next():如果迭代中具有更多元素,则返回true import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /* * lterator:迭代器,集合的专用遍历方式 * lterator<E>iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到 *迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖集合而存在的 * *lterator中的常用方法 *E next():返回迭代中的下一个元素 *blooean has Next():如果迭代中具有更多元素,则返回true * * */ public class Collection3 { public static void main(String[] args) { //创建集合对象 Collection<String> c =new ArrayList<String>(); //添加元素 c.add("hello"); c.add("world"); c.add("java"); // lterator<E>iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到 Iterator<String> it = c.iterator(); /* * public Iterator<E> iterator() { return new Itr(); } private class Itr implements Iterator<E> { ... } */ //E next():返回迭代中的下一个元素 /* System.out.println(it.next()); System.out.println(it.next()); System.out.println(it.next()); System.out.println(it.next());//NoSuchElementException:表示被请求的元素不存在 */ //blooean has Next():如果迭代中具有更多元素,则返回true /* if(it.hasNext()) { System.out.println(it.next()); } if(it.hasNext()) { System.out.println(it.next()); } if(it.hasNext()) { System.out.println(it.next()); } if(it.hasNext()) { System.out.println(it.next()); } */ while(it.hasNext()) { // System.out.println(it.next()); String s = it.next(); System.out.println(s); } } }
    Processed: 0.015, SQL: 9