java基础的三大集合体系_Java 基础 33 集合的概述和Collection集合

1.1 集合的概述

集合是一个容器,是用来存储和获取数据的.

1.1.1 为什么会出现集合类

为什么出现集合类:

我们学习的是面向对象的编程语言,面向对象的编程语言对事物的描述都是通过对象体现的,

为了方便对多个对象进行操作,我们就必须把这多个对象进行存储,而要想存储多个对象,就不能是基本的变量了,应该是一个容器类型的变量。

回顾我们学过的知识,有哪些是容器类型的呢?

数组

StringBuilder

首先说StringBuilder,它的结果是一个字符串,不一定满足我们的需求,所以我们只能

选择数组了,而数组的长度固定,不能适应变化的需求,在这种情况下,Java就提供了集合类供我们使用。

由此可见,集合类的长度是可变的。

集合类的特点:长度可变。

1.1.2集合类体系结构图

87d103a0026d

Collection.png

1.2 Collection集合

1.2.1 创建Collection集合对象并添加元素

Collection:是单列集合的顶层接口。

Collection 表示一组对象,这些对象也称为 collection 的元素。

一些 collection 允许有重复的元素,而另一些则不允许。

一些 collection 是有序的,而另一些则是无序的。

JDK 不提供此接口的任何直接

实现:它提供更具体的子接口(如 Set 和 List)实现。

创建Collection集合的对象,我们采用的是多态的方式,使用的是具体的ArrayList类。

因为这个类是最常用的集合类。

ArrayList()

Collection:

:是一种特殊的数据类型,泛型。这里我们会使用就可以了。

如何使用呢?

在出现E的地方用引用数据类型替换即可。

举例:Collection,Collection

1.2.1.1 案例代码

package com.itheima_01;

import java.util.ArrayList;

import java.util.Collection;

/*

* Collection:是单列集合的顶层接口。

* Collection 表示一组对象,这些对象也称为 collection 的元素。

* 一些 collection 允许有重复的元素,而另一些则不允许。

* 一些 collection 是有序的,而另一些则是无序的。

* JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。

*

* 创建Collection集合的对象,我们采用的是多态的方式,使用的是具体的ArrayList类。

* 因为这个类是最常用的集合类。

* ArrayList()

*

* Collection:

* :是一种特殊的数据类型,泛型。这里我们会使用就可以了。

* 如何使用呢?

* 在出现E的地方用引用数据类型替换即可。

* 举例:Collection,Collection

*/

public class CollectionDemo {

public static void main(String[] args) {

//创建Collection集合对象

//JDK7的新特性,看懂就可以

//Collection c = new ArrayList<>(); //多态的方式

Collection c = new ArrayList(); //多态的方式

//boolean add(E e):添加元素

c.add("hello");

c.add("world");

c.add("java");

//输出集合对象

System.out.println(c);

//输出了集合中的元素按照指定格式拼接的内容,说明ArrayList重写了toString()方法

}

}

1.2.2 Collection集合的成员方法

boolean add(E e):添加元素

boolean remove(Object o):从集合中移除元素

void clear():清空集合中的元素

boolean contains(Object o):判断集合中是否存在指定的元素

boolean isEmpty():判断集合是否为空

int size():集合的长度,也就是集合中元素的个数

1.2.2.1 案例代码

package com.itheima_01;

import java.util.ArrayList;

import java.util.Collection;

/*

* boolean add(E e):添加元素

* boolean remove(Object o):从集合中移除元素

* void clear():清空集合中的元素

* boolean contains(Object o):判断集合中是否存在指定的元素

* boolean isEmpty():判断集合是否为空

* int size():集合的长度,也就是集合中元素的个数

*/

public class CollectionDemo2 {

public static void main(String[] args) {

//创建集合对象

Collection c = new ArrayList();

//boolean add(E e):添加元素

//System.out.println("add:"+c.add("hello"));

//System.out.println("add:"+c.add("world"));

//通过查看源码,我们知道ArrayList集合的add方法的返回值永远都是true

c.add("hello");

c.add("world");

c.add("java");

//boolean remove(Object o):从集合中移除元素

//System.out.println("remove:"+c.remove("world"));

//System.out.println("remove:"+c.remove("haha"));

//void clear():清空集合中的元素

//c.clear();

//boolean contains(Object o):判断集合中是否存在指定的元素

//System.out.println("contains:"+c.contains("world"));

//System.out.println("contains:"+c.contains("haha"));

//boolean isEmpty():判断集合是否为空

//System.out.println("isEmpty:"+c.isEmpty());

//int size():集合的长度,也就是集合中元素的个数

System.out.println("size:"+c.size());

//输出集合对象

System.out.println(c);

}

}

1.2.3 Collection集合的遍历

Collection集合的遍历

Iterator iterator():返回在此 collection 的元素上进行迭代的迭代器。

通过集合对象调用iterator()方法得到迭代器对象。

Iterator:

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

boolean hasNext():如果仍有元素可以迭代,则返回 true。

1.2.3.1 案例代码

package com.itheima_01;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

/*

* Collection集合的遍历

*

* Iterator iterator():返回在此 collection 的元素上进行迭代的迭代器。

* 通过集合对象调用iterator()方法得到迭代器对象。

*

* Iterator:

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

* boolean hasNext():如果仍有元素可以迭代,则返回 true。

*/

public class CollectionDemo3 {

public static void main(String[] args) {

//创建集合对象

Collection c = new ArrayList();

//添加元素

c.add("hello");

c.add("world");

c.add("java");

//Iterator iterator()

Iterator it = c.iterator();//返回的是迭代器接口的实现类的对象

//System.out.println(it.next());

//System.out.println(it.next());

//System.out.println(it.next());

//NoSuchElementException:没有这样的元素异常

//System.out.println(it.next());

//boolean hasNext()

while(it.hasNext()){

//System.out.println(it.next());

String s = it.next();

System.out.println(s);

}

}

}

1.2.4 集合使用步骤图解

87d103a0026d

Collection2.png

1.2.5 Collection集合的练习存储自定义对象并遍历

Collection集合存储自定义对象并遍历

提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。

集合的使用步骤:

创建集合对象

创建元素对象

把元素添加到集合

遍历集合

1.2.5.1 案例代码

package com.itheima_02;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

/*

* Collection集合存储自定义对象并遍历

* 提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。

*

* 集合的使用步骤:

* A:创建集合对象

* B:创建元素对象

* C:把元素添加到集合

* D:遍历集合

*/

public class CollectionTest {

public static void main(String[] args) {

//创建集合对象

Collection c = new ArrayList();

//创建元素对象

Student s1 = new Student("林青霞",30);

Student s2 = new Student("张曼玉",35);

Student s3 = new Student("王祖贤",33);

//把元素添加到集合

c.add(s1);

c.add(s2);

c.add(s3);

//遍历集合

Iterator 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、付费专栏及课程。

余额充值