Collection体系集合

集合

什么是集合

  • 概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能(类似数组功能)。
  • 和数组区别:
    • (1)数组长度固定,集合长度不固定
    • (2)数组可以存储基本类型和引用类型,集合只能存储引用类型
  • 位置:java.util.*;

Collection体系集合

  • interface Collection(该体系结构的根接口,代表一组对象,称为“集合”。)
    • interface List(List接口的特定:有序、有下标、元素可重复)
      • Class ArrayList
      • Class LinkedList
      • Class Vector
    • interface Set(Set接口的特点:无序、无下标,元素不能重复)
      • Class HashSet
      • interface SortedSet
        • class TreeSet

Collection父接口

  • 特点:代表一组任意类型的对象,无序、无下标、不能重复。
  • 方法:
    • boolean add(Object obj) //添加一个对象。
    • boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中。
    • void clear() //清空此集合中的所有对象
    • boolean contains(Object o) //检查此集合中是否包含o对象
    • boolean equals(Object o) //比较此集合是否与指定对象相等。
    • boolean isEmpty() //判断此集合是否为空。
    • boolean remove(Object o) //在此集合中移除o对象。
    • int size() //返回此集合中的元素个数。
    • Object[] toArray() //将此集合转换成数组。
package com.collectionsFramework.demo01;
/**
* 学生类
*/
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.collectionsFramework.demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* collection接口的使用
* (1)添加元素
* (2)删除元素
* (3)遍历元素
* (4)判断
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList();
// * (1)添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("榴莲");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
// * (2)删除元素
// collection.remove("榴莲");
// collection.clear();
// System.out.println("删除之后:"+collection.size());
// * (3)遍历元素
//3.1使用增强for(for循环有下标,不用)
System.out.println("------------3.1使用增强for------------");
for (Object object:collection) {
System.out.println(object);
}
//3.2使用迭代器(迭代器专门用来遍历集合的一种方法)
//hasNext();有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
System.out.println("------------3.2使用迭代器------------");
Iterator it = collection.iterator();
while (it.hasNext()){
String s = (String)it.next();
System.out.println(s);
//collection.remove(s);//ConcurrentModificationException并发修改异常,迭代过程中不允许使用collection删除方法
// it.remove();
}
System.out.println("元素个数:"+collection.size());
// * (4)判断
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
}
}
package com.collectionsFramework.demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
*collection的使用:保存学生信息
*/
public class Demo02 {
public static void main(String[] args) {
//新建collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三",20);
Student s2 = new Student("张无忌",18);
Student s3 = new Student("王二",22);
//1、添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//删除
// collection.remove(s1);//2
// collection.remove(new Student("王二",22));//3,并没有删除
// collection.clear();//只是把集合里的所在地址删除了,对象并没有被删除
System.out.println("删除之后:"+collection.size());
//3.1增强for
System.out.println("---------增强for---------");
for (Object object:collection) {
Student s = (Student) object;
System.out.println(s.toString());
}
//3.2迭代器:hasNext(); next(); remove();迭代过程中不能使用collection的删除方法
System.out.println("---------迭代器---------");
Iterator it = collection.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
//判断
System.out.println(collection.contains(s1));
System.out.println(collection.isEmpty());
}
}

笔记出处

posted @   时间染指  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示