Java中的Collection容器

1.1集合知识回顾

集合类的特点:提供一种可变存储空间可变的存储模型,存储的数据容量可以随时发生改变。

1.2集合类体系结构

集合:
Collection单列:
List(可重复)->ArrayList、LinkList
set(不可重复)->HashSet、TreeSet
Map双列:HashMap

1.3Collection集合概述和使用

collection是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素;
JDK不提供此接口的任何直接实现,它提供更具体的子接口(如set和list)实现。
创建Collection集合对象:
1.多态的方式
2.具体的实现类ArrayList

1.4Collection集合常用方法

常用方法示例

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

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

public class CollectionDemo01 {
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();
        c.add("my");
        c.add("name");
        c.add("is");
        c.add("luna");
        System.out.println(c);
        System.out.println(c.size());
        c.remove("luna");
        System.out.println(c);
        System.out.println(c.contains("my"));
        System.out.println(c.isEmpty());
        c.clear();
        System.out.println(c);
    }
}

输出结果:

[my, name, is, luna]
4
[my, name, is]
true
false
[]

1.5Collection集合的遍历

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

Interface Iterator
iterator() 此方法返回集合中元素的迭代器;
next:返回迭代器中的下一个元素;
hasNext:如果迭代具有更多元素,则返回true。

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

public class CollectionDemo01 {
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();
        c.add("my");
        c.add("name");
        c.add("is");
        c.add("luna");
        System.out.println(c);
        Iterator<String> it = c.iterator();
        System.out.println(it.next());
        System.out.println(it.hasNext());
        System.out.println(it.next());
    }
}

输出结果:

[my, name, is, luna]
my
true
name

循环输出:

        for(String s:c)
            if (it.hasNext()){
                System.out.println(it.next());
            }
 		while (it.hasNext()){
               String s=it.next();
               System.out.println(s);
           }

1.6集合的使用步骤

1.先创建集合对象
2.添加元素
创建元素->添加元素到集合
3.遍历集合
通过集合对象获取迭代器对象
通过迭代器对象的hasNext()方法判断是否还有元素
通过迭代器的next()方法获取下一个元素

Collection集合存储学生对象并遍历

需求:创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
思路:
1.创建一个学生类
2.创建一个Collection集合对象
3.创建学生对象
4.把学生添加到集合
5.遍历集合(迭代器方式)

package daily_collection;

public class Student {
    private String name;
    private int age;

    public Student(){
    }

    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
package daily_collection;

import javafx.print.Collation;

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

public class CollectionDemo02 {
    public static void main(String[] args) {
        Collection<Student> c=new ArrayList<Student>();
        Student s1=new Student("TOM",21);
        Student s2=new Student("JERRY",20);
        Student s3=new Student("BOB",23);

        c.add(s1);
        c.add(s2);
        c.add(s3);
        System.out.println(c);

        Iterator<Student> it=c.iterator();
        while(it.hasNext()){
            Student s=it.next();
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值