###集合(1)###
1. 集合的概述:java中的集合类是一种工具类,就像是容器,储存任意数据的具有共同性质的对象
集合的作用:
1)在类的内部,对数据进行组织;
2)简单而快速的搜索大数量的条目;
3)有的集合接口提供了一些列排列有序的元素,并且可以在序列中间快速的插入或者删除有关元素;
4)有的集合接口,提供了映射关系,可以通过关键字(key)去快速查找到对应的唯一对象,而这个关键字可以是任意类型;
集合与数组:
1)数组的长度固定,集合集合的长度可变
2)数组只能ton通过下表访问元素,类型固定,而有的集合可以通过任意类型查找所映射的具体对象
集合框架体系结构:
1) Collection:
List(序列):排列有序且可以重复
1) ArrayList:数组序列
2) LinkedList:链表
Queue(队列)有序且可以重复(不常用)
LinkedList
Set(集):无序且不可重复
HashSet:哈希集
2)Map: --------> <Key , value> : Entry(键值对)
HashMap:哈希表
2. Collection接口与List接口
Collection接口:
> 是List、Set和Queue接口的父接口
> 定义了可用于操作的List、Set和Queue的方法 ------> 增删改查
1)List接口及其实现类 ---> ArrayList
> List是元素有序并且可以重复的集合,被称为序列
> List是可以精确的控制每隔元素的插入位置,或删除某个位置的元素
> ArrayList --- 数组序列,是List的一个重要实现类
> ArrayList底层是由数组实现的
例题:模拟学生选课
需求:选择课程(向集合中添加课程),删除所选的某门课,查看所选课程,修改所选课程
1)add方法的使用
public class Course {
private String id;
private String name;
public Course(String id, String name) {
this.id = id;
this.name = name;
}
public Course() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.HashSet;
import java.util.Set;
public class Student {
private String id;
private String name;
public Set course;
public Student() {
}
public Student(String id, String name) {
this.id = id;
this.name = name;
this.course = new HashSet();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.List;
public class ListTest {
//用于存放备选课程的List
public List coursesToSelct;
public ListTest(){
this.coursesToSelct = new ArrayList();
}
//用于往coursesToSelect中添加备选课程
public void testAdd(){
//创建一个课程对象,并通过调用add方法,添加到备选课程的List中
Course cr1 = new Course("1","JavaSe");
coursesToSelct.add(cr1);
//对象存入集合都编程Object类型去除时需要类型转换
Course temp1 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:"+temp1.getId()+":"+temp1.getName());
Course cr2 = new Course("2","JavaEE");
coursesToSelct.add(0,cr2);
Course temp2 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:"+temp2.getId()+":"+temp2.getName());
}
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
}
}
思考:当调用add方法时传递进去的位置参数大于当前长度会如何?
import java.util.ArrayList;
import java.util.List;
public class ListTest {
//用于存放备选课程的List
public List coursesToSelct;
public ListTest(){
this.coursesToSelct = new ArrayList();
}
//用于往coursesToSelect中添加备选课程
public void testAdd(){
//创建一个课程对象,并通过调用add方法,添加到备选课程的List中
Course cr1 = new Course("1","JavaSe");
coursesToSelct.add(cr1);
//对象存入集合都编程Object类型去除时需要类型转换
Course temp1 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:"+temp1.getId()+":"+temp1.getName());
Course cr2 = new Course("2","JavaEE");
coursesToSelct.add(0,cr2);
Course temp2 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:"+temp2.getId()+":"+temp2.getName());
Course cr3 = new Course("3","C语言");
coursesToSelct.add(4,cr3);//给出溢出位置
}
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
}
}
从上可以看出:数组下标越界异常
2)addAll方法
1>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListTest {
//用于存放备选课程的List
public List coursesToSelct;
public ListTest() {
this.coursesToSelct = new ArrayList();
}
//用于往coursesToSelect中添加备选课程
public void testAdd() {
//创建一个课程对象,并通过调用add方法,添加到备选课程的List中
Course cr1 = new Course("1", "JavaSe");
coursesToSelct.add(cr1);
//对象存入集合都编程Object类型去除时需要类型转换
Course temp1 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:" + temp1.getId() + ":" + temp1.getName());
Course cr2 = new Course("2", "JavaEE");
coursesToSelct.add(0, cr2);
Course temp2 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:" + temp2.getId() + ":" + temp2.getName());
Course[] courses = {new Course("3", "C语言"), new Course("4", "python")};
coursesToSelct.addAll(Arrays.asList(courses));
Course temp3 = (Course) coursesToSelct.get(2);
Course temp4 = (Course) coursesToSelct.get(3);
System.out.println("所添加的两门课程为:" + "\n\t" + temp3.getId() + ":" + temp3.getName() + "\n\t" + temp4.getId() + ":" + temp4.getName());
}
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
}
}
2>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListTest {
//用于存放备选课程的List
public List coursesToSelct;
public ListTest() {
this.coursesToSelct = new ArrayList();
}
//用于往coursesToSelect中添加备选课程
public void testAdd() {
//创建一个课程对象,并通过调用add方法,添加到备选课程的List中
Course cr1 = new Course("1", "JavaSe");
coursesToSelct.add(cr1);
//对象存入集合都编程Object类型去除时需要类型转换
Course temp1 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:" + temp1.getId() + ":" + temp1.getName());
Course cr2 = new Course("2", "JavaEE");
coursesToSelct.add(0, cr2);
Course temp2 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:" + temp2.getId() + ":" + temp2.getName());
Course[] course1 = {new Course("3", "C语言"), new Course("4", "python")};
coursesToSelct.addAll(Arrays.asList(course1));
Course temp3 = (Course) coursesToSelct.get(2);
Course temp4 = (Course) coursesToSelct.get(3);
System.out.println("所添加的两门课程为:" + "\n\t" + temp3.getId() + ":" + temp3.getName() +
"\n\t" + temp4.getId() + ":" + temp4.getName());
Course[] course2 = {new Course("5", "Html"), new Course("6", "Css")};
coursesToSelct.addAll(2,Arrays.asList(course2));
Course temp5 = (Course)coursesToSelct.get(2);
Course temp6 = (Course)coursesToSelct.get(3);
System.out.println("所添加的两门课程为:" + "\n\t" + temp5.getId() + ":" + temp5.getName() +
"\n\t" + temp6.getId() + ":" + temp6.getName());
}
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
}
}
其中在coursesToSelct中的储存位置为:
3)取出List中的元素(遍历)
1>普通方法
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListTest {
//用于存放备选课程的List
public List coursesToSelct;
public ListTest() {
this.coursesToSelct = new ArrayList();
}
//用于往coursesToSelect中添加备选课程
public void testAdd() {
//创建一个课程对象,并通过调用add方法,添加到备选课程的List中
Course cr1 = new Course("1", "JavaSe");
coursesToSelct.add(cr1);
//对象存入集合都编程Object类型去除时需要类型转换
Course temp1 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:" + temp1.getId() + ":" + temp1.getName());
Course cr2 = new Course("2", "JavaEE");
coursesToSelct.add(0, cr2);
Course temp2 = (Course) coursesToSelct.get(0);
System.out.println("添加了课程:" + temp2.getId() + ":" + temp2.getName());
Course[] course1 = {new Course("3", "C语言"), new Course("4", "python")};
coursesToSelct.addAll(Arrays.asList(course1));
Course temp3 = (Course) coursesToSelct.get(2);
Course temp4 = (Course) coursesToSelct.get(3);
System.out.println("所添加的两门课程为:" + "\n\t" + temp3.getId() + ":" + temp3.getName() +
"\n\t" + temp4.getId() + ":" + temp4.getName());
Course[] course2 = {new Course("5", "Html"), new Course("6", "Css")};
coursesToSelct.addAll(2, Arrays.asList(course2));
Course temp5 = (Course) coursesToSelct.get(2);
Course temp6 = (Course) coursesToSelct.get(3);
System.out.println("所添加的两门课程为:" + "\n\t" + temp5.getId() + ":" + temp5.getName() +
"\n\t" + temp6.getId() + ":" + temp6.getName());
}
public void testGet() {
System.out.println("------------------");
System.out.println("待选课程为:");
for (int i = 0; i < coursesToSelct.size(); i++) {
Course cr = (Course) coursesToSelct.get(i);
System.out.println("课程:" + cr.getId() + ":" + cr.getName());
}
}
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
}
}
2> 通过迭代器遍历(只用来遍历集合中元素,自身无存储功能)
//通过迭代器来遍历iterator
public void testIterator() {
System.out.println("------------------");
System.out.println("(迭代器)待选课程为:");
Iterator it = coursesToSelct.iterator();
while (it.hasNext()) {
Course cr = (Course) it.next();
System.out.println("课程:" + cr.getId() + ":" + cr.getName());
}
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
lt.testIterator();
}
}
3>通过 for each方法遍历
public void testForEach() {
System.out.println("------------------");
System.out.println("(ForEach)待选课程为:");
for (Object obj : coursesToSelct) {
Course cr = (Course) obj;
System.out.println("课程:" + cr.getId() + ":" + cr.getName());
}
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
lt.testIterator();
lt.testForEach();
}
}
4)修改元素
public void testModify() {
coursesToSelct.set(4, new Course("7", "JavaScript"));
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
lt.testIterator();
lt.testForEach();
lt.testModify();
lt.testForEach();
}
}
5)删除元素
1>
public void testRemove(){
System.out.println("------------------");
Course cr = (Course) coursesToSelct.get(4);
System.out.println("课程 "+cr.getId()+":"+cr.getName()+" 被删除");
coursesToSelct.remove(cr);
System.out.println("成功删除!");
testForEach();
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
lt.testIterator();
lt.testForEach();
lt.testModify();
lt.testForEach();
lt.testRemove();
}
}
2>
public void testRemove(){
System.out.println("------------------");
System.out.println("删除4位置上的课程");
coursesToSelct.remove(4);
System.out.println("成功删除!");
testForEach();
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
lt.testIterator();
lt.testForEach();
lt.testModify();
lt.testForEach();
lt.testRemove();
}
}
3> removeAll
public void testRemove() {
System.out.println("------------------");
System.out.println("删除4、5位置上的课程");
Course[] courses = {(Course) coursesToSelct.get(4), (Course) coursesToSelct.get(5)};
System.out.println("成功删除!");
testForEach();
}
public class Ex1 {
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
lt.testIterator();
lt.testForEach();
lt.testModify();
lt.testForEach();
lt.testRemove();
}
}
####END####