集合基础详细框架
一、大致包括六个部分
1.集合的概念
2.Collection接口
3.List接口与实现类
4.泛型和工具类
5.Set接口与实现类
6.Map接口与实现类
一、集合是什么?
- 概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。
- 和数组的区别:
(1)数组长度可以固定,集合长度不固定
(2)数组可以存储基本类型和引用类型,集合只能存储引用类型
二、Collection接口
1.Collection体系集合
代码如下(示例1):
public class Demo01 {
public static void main(String[] args) {
//创建集合
Collection co = new ArrayList();
//1.添加元素
co.add("春天");
co.add("夏天");
co.add("秋天");
co.add("冬天");
System.out.println("元素个数: "+co.size());
System.out.println(co);
//2.删除元素
// co.remove("冬天");
// System.out.println("元素个数: "+co.size());
// System.out.println(co);
// co.clear();
// System.out.println("元素个数: "+co.size());
//3.遍历元素
//3.1使用增强for循环
for (Object object:co){
System.out.println(object);
}
//3.2使用迭代器
//hasNext();有没有下一个元素
//next();读取下一个元素
//remove();删除当前元素
System.out.println("*---------------*");
Iterator it= co.iterator();
while (it.hasNext()){
String s=(String)it.next();
System.out.println(s);
// co.remove("冬天"); 不能使用
// it.remove();
}
// System.out.println(co.size());
System.out.println(co.contains("冬天"));
System.out.println(co.isEmpty());
}
}
2.保存学生信息
代码如下(示例2):
public class Demo02 {
public static void main(String[] args) {
//新建collection对象
Collection collection = new ArrayList();
Student s1=new Student("锐雯",5);
Student s2=new Student("亚瑟",6);
Student s3=new Student("盲僧",9);
//添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数: "+collection.size());
System.out.println(collection.toString());
//删除
collection.remove(s1);
System.out.println("删除之后:"+collection.size());
System.out.println(collection.toString());
// collection.clear();
// System.out.println(collection.size());
//遍历
System.out.println("------增强for遍历--------");
for (Object object:collection){
Student s=(Student) object;
System.out.println(s.toString());
}
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(s2));
System.out.println(collection.contains(s1));
System.out.println(collection.isEmpty());
}
}
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name, int age) {
super();
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+"]";
}
}
List子接口
- 特点:有序、有下标、元素可以重复
- 方法:
- void add(int index, Object o) //在index位置插入对象o
- bollean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置
- Object get(int index) /返回集合中指定位置的元素
- List subList(int fromIndex,int toIndex) //返回fromIndex和toIndex之间的集合元素
public class Demo03 {
public static void main(String[] args) {
//先创建集合对象
List list=new ArrayList<>();
//添加元素
list.add("苹果");
list.add("小米");
list.add(0,"华为");
list.add(2,"OPPO");
System.out.println("元素个数: "+list.size());
System.out.println(list.toString());
//删除元素
System.out.println("*-------------*");
list.remove(0);
System.out.println("删除之后元素个数: "+list.size());
System.out.println(list.toString());
//遍历元素
System.out.println("*-------------------*");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("*-----------------*");
for (Object object :list){
System.out.println(object);
}
System.out.println("*--------------------*");
Iterator it=list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//和Iterator的区别,ListIterator可以向前或向后遍历,添加,删除,修改元素
System.out.println("*-------列表迭代器-----------*");
ListIterator lit = list.listIterator();
//从前往后
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
System.out.println("****************");
//从后往前
while (lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//判断
System.out.println(list.contains("OPPO"));
System.out.println(list.isEmpty());
//获取位置
System.out.println(list.indexOf("苹果"));
}
}
package com.Collection;
import java.util.ArrayList;
import java.util.List;
public class Demo04 {
public static void main(String[] args) {
//创建集合
List list = new ArrayList();
//添加数字数据(自动装箱)
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(99);
System.out.println("元素个数: "+list.size());
System.out.println(list.toString());
//删除操作
list.remove(2);//指的是数组下标
System.out.println(list.toString());
list.remove((Object)99);
// list.remove(new Integer(20));
System.out.println(list.toString());
//补充方法subList:返回子集合 含头不含尾
List subList= list.subList(1,3);
System.out.println(subList.toString());
}
}
- 下节讲述List实现类
- ArrayList的使用
- vector的使用