Collection集合-Java

Collection

(1)是单列集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素;

(2)JDK不提供此接口的任何直接实现,它提供更具体的子接口(List和Set)实现;

创建Collectionde集合的对象

(1)多态的方式;(2)具体的实现类ArrayList

public class Collection_1 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");
        System.out.println(c);
    }
}

Collection 集合的常用方法

方法名说明
boolean add(E e)添加元素
boolean remove(E e)从集合中移除指定元素
void clear()清空集合中的元素
boolean contains(Object o)判断集合中是否存在指定的元素
boolean isEmpty()判断集合是否为空
int size()返回集合的长度,也就是集合中元素的个数
package Collection_practice;

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

public class Collection_2 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<String>();
        c.add("pjh");
        c.add("tjs");
        c.add("sjl");
        c.add("qcx");
        System.out.println(c);
        c.remove("qcx");
        System.out.println(c);
        System.out.println(c.contains("tjs"));
        System.out.println(c.isEmpty());
        System.out.println(c.size());
        c.clear();
        System.out.println(c);

    }
}

Collection集合的遍历

Iterator:迭代器,集合的专用遍历方式。

(1)Iterator <E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到;

(2)迭代器是通过集合的iterator()方法得到的,所以它是依赖于集合而存在的。

Iterator中常用的方法:

E next():返回迭代中的下一个元素

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

package Collection_practice;

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

public class Collection_3 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<String>();
        c.add("pjh");
        c.add("tjs");
        c.add("sjl");
        c.add("qcx");
        Iterator<String> it = c.iterator();
      /*  System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());*/
        while (it.hasNext()){
            /*System.out.println(it.next());*/
            String s = it.next();
            System.out.println(s);
        }

    }

}

集合的使用步骤:

步骤1:创建集合对象

Collection<String> c = new ArrayList<String>();

步骤2:添加元素

String s = "pjh";

c.add(s);

步骤3:遍历集合

(1)通过集合对象获取迭代器对象;Iterator<String> it = c.iterator();

(2)通过迭代器对象的hasNext()方法判断是否还有元素;while(it.hasNext())

(3)通过迭代器对象的next()方法获取下一个元素。String s = it.next();

实例:

package Collection_practice;

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

public class Collection_4 {
    public static void main(String[] args) {
        Collection<student> stu = new ArrayList<student>();
        student stu1 = new student();
        stu1.setName("pjh");
        stu1.setAge(23);
        stu1.setAddress("chongqing");
        stu1.setNumber("s200101205");
        student stu2 = new student();
        stu2.setName("tjs");
        stu2.setAge(21);
        stu2.setAddress("shanghai");
        stu2.setNumber("s200101206");
        student stu3 = new student();
        stu3.setName("sjl");
        stu3.setAge(24);
        stu3.setAddress("changsha");
        stu3.setNumber("s200101207");
        stu.add(stu1);
        stu.add(stu2);
        stu.add(stu3);
        Iterator<student> it = stu.iterator();
        while (it.hasNext()){
            student Stu = it.next();
            System.out.println("姓名:"+Stu.getName()+" ,学号:"+Stu.getNumber()+" ,年龄:"+Stu.getAge()+" ,居住地:"+Stu.getAddress());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值