• Java中的集合框架概述

集合的概念:

Java中的集合类:是一种工具类,就像是容器,存储任意数量的具有共同属性的对象。

集合的作用:

1.在类的内部,对数据进行组织;

2.简单的快速的搜索大数据量的条目;

3.有的集合接口,提供了一系列排列有序的元素,并且 可以在序列中间快速的插入或删除有关的元素。

4.有的集合接口,提供了映射关系,可以通过 关键字(key)去快速查找到对应的唯一对象,而这个关键字可以是任意类型。

与数组的对比一为何选择集合而不是数组

1.数组的长度固定,集合长度可变

2.数组只能通过下标访问元素,类型固定,而有的集合可以通过任意类型查找所影射的具体对象

Java集合体系结构

Collection家族和Map家族组成。

  • Collection接口&List接口简介

Collection接口子接口以及实现类

Collection接口

1.是List、Set和Queue接口的父接口

2.定义了可用于操作List、Set和Queue的方法一增删改查

List接口及其实现类——ArrayList

1.List是元素有序并且可以重复的集合,被称为序列

2.List可以精确的控制每个元素的插入位置,或删除某个位置元素

3.ArrayList——数组序列,是List的一个重要的实现类

4.ArryList底层是由数组实现的

  • 学生选课——创建学生类和课程类

1.创建课程类(Course)

2.创建学生类

  • 学生选课——添加课程1
  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course c=new Course("1","数据结构");
  23. CourseToSelect.add(c);
  24. Course temp=(Course) CourseToSelect.get(0);
  25. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  26.  
  27. Course c1=new Course("2","C语言");
  28. CourseToSelect.add(0, c1);
  29. Course temp1=(Course) CourseToSelect.get(0);
  30. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  31.  
  32. Course c2=new Course("2","Test1");
  33. CourseToSelect.add(4, c2);
  34. }
  35.  
  36. public static void main(String []args){
  37. ListTest list=new ListTest();
  38. list.addCourse();
  39. }
  40. }

运行结果为:

  • 学生选课——添加课程11
  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  23. CourseToSelect.addAll(Arrays.asList(course));
  24. Course temp2=(Course) CourseToSelect.get(2);
  25. Course temp3=(Course) CourseToSelect.get(3);
  26. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  27.  
  28. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  29. CourseToSelect.addAll(2, Arrays.asList(course2));
  30. Course temp4=(Course) CourseToSelect.get(2);
  31. Course temp5=(Course) CourseToSelect.get(3);
  32. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  33.  
  34. }
  35.  
  36. public static void main(String []args){
  37. ListTest list=new ListTest();
  38. list.addCourse();
  39. }
  40. }

运行结果为:

  • 学生选课——查询课程

取得List中的元素的方法

  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course c=new Course("1","数据结构");
  23. CourseToSelect.add(c);
  24. Course temp=(Course) CourseToSelect.get(0);
  25. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  26.  
  27. Course c1=new Course("2","C语言");
  28. CourseToSelect.add(0, c1);
  29. Course temp1=(Course) CourseToSelect.get(0);
  30. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  31.  
  32. Course cq=new Course("1","数据结构");
  33. CourseToSelect.add(cq);
  34. Course temp0=(Course) CourseToSelect.get(2);
  35. System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
  36.  
  37. /*Course c2=new Course("2","Test1");
  38. CourseToSelect.add(4, c2);*/
  39. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  40. CourseToSelect.addAll(Arrays.asList(course));
  41. Course temp2=(Course) CourseToSelect.get(2);
  42. Course temp3=(Course) CourseToSelect.get(3);
  43. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  44.  
  45. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  46. CourseToSelect.addAll(2, Arrays.asList(course2));
  47. Course temp4=(Course) CourseToSelect.get(2);
  48. Course temp5=(Course) CourseToSelect.get(3);
  49. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  50.  
  51. }
  52. /*
  53. * 取得List中的元素的方法
  54. * */
  55. public void testGet(){
  56. System.out.println("有如下课程待选:");
  57. int size=CourseToSelect.size();
  58. for (int i = 0; i < size; i++) {
  59. Course cr=(Course) CourseToSelect.get(i);
  60. System.out.println("课程:"+cr.id+":"+cr.name);
  61. }
  62. }
  63.  
  64. public static void main(String []args){
  65. ListTest list=new ListTest();
  66. list.addCourse();
  67. list.testGet();
  68. }
  69. }

运行结果为:

  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course c=new Course("1","数据结构");
  23. CourseToSelect.add(c);
  24. Course temp=(Course) CourseToSelect.get(0);
  25. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  26.  
  27. Course c1=new Course("2","C语言");
  28. CourseToSelect.add(0, c1);
  29. Course temp1=(Course) CourseToSelect.get(0);
  30. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  31.  
  32. Course cq=new Course("1","数据结构");
  33. CourseToSelect.add(cq);
  34. Course temp0=(Course) CourseToSelect.get(2);
  35. System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
  36.  
  37. /*Course c2=new Course("2","Test1");
  38. CourseToSelect.add(4, c2);*/
  39. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  40. CourseToSelect.addAll(Arrays.asList(course));
  41. Course temp2=(Course) CourseToSelect.get(2);
  42. Course temp3=(Course) CourseToSelect.get(3);
  43. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  44.  
  45. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  46. CourseToSelect.addAll(2, Arrays.asList(course2));
  47. Course temp4=(Course) CourseToSelect.get(2);
  48. Course temp5=(Course) CourseToSelect.get(3);
  49. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  50.  
  51. }
  52. /*
  53. * 取得List中的元素的方法
  54. * */
  55. public void testGet(){
  56. System.out.println("有如下课程待选:");
  57. int size=CourseToSelect.size();
  58. for (int i = 0; i < size; i++) {
  59. Course cr=(Course) CourseToSelect.get(i);
  60. System.out.println("课程:"+cr.id+":"+cr.name);
  61. }
  62. }
  63.  
  64. /*
  65. * 通过迭代器来遍历List
  66. * */
  67. public void testIterator(){
  68. //通过集合iterator方法,取得迭代器实例
  69. Iterator it=CourseToSelect.iterator();
  70. System.out.println("有如下课程待选(通过迭代器访问):");
  71. while (it.hasNext()) {
  72. Course cr = (Course) it.next();
  73. System.out.println("课程:"+cr.id+":"+cr.name);
  74. }
  75. }
  76.  
  77. public static void main(String []args){
  78. ListTest list=new ListTest();
  79. list.addCourse();
  80. list.testGet();
  81. list.testIterator();
  82. }
  83. }

运行结果为:

  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course c=new Course("1","数据结构");
  23. CourseToSelect.add(c);
  24. Course temp=(Course) CourseToSelect.get(0);
  25. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  26.  
  27. Course c1=new Course("2","C语言");
  28. CourseToSelect.add(0, c1);
  29. Course temp1=(Course) CourseToSelect.get(0);
  30. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  31.  
  32. Course cq=new Course("1","数据结构");
  33. CourseToSelect.add(cq);
  34. Course temp0=(Course) CourseToSelect.get(2);
  35. System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
  36.  
  37. /*Course c2=new Course("2","Test1");
  38. CourseToSelect.add(4, c2);*/
  39. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  40. CourseToSelect.addAll(Arrays.asList(course));
  41. Course temp2=(Course) CourseToSelect.get(2);
  42. Course temp3=(Course) CourseToSelect.get(3);
  43. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  44.  
  45. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  46. CourseToSelect.addAll(2, Arrays.asList(course2));
  47. Course temp4=(Course) CourseToSelect.get(2);
  48. Course temp5=(Course) CourseToSelect.get(3);
  49. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  50.  
  51. }
  52. /*
  53. * 取得List中的元素的方法
  54. * */
  55. public void testGet(){
  56. System.out.println("有如下课程待选:");
  57. int size=CourseToSelect.size();
  58. for (int i = 0; i < size; i++) {
  59. Course cr=(Course) CourseToSelect.get(i);
  60. System.out.println("课程:"+cr.id+":"+cr.name);
  61. }
  62. }
  63.  
  64. /*
  65. * 通过迭代器来遍历List
  66. * */
  67. public void testIterator(){
  68. //通过集合iterator方法,取得迭代器实例
  69. Iterator it=CourseToSelect.iterator();
  70. System.out.println("有如下课程待选(通过迭代器访问):");
  71. while (it.hasNext()) {
  72. Course cr = (Course) it.next();
  73. System.out.println("课程:"+cr.id+":"+cr.name);
  74. }
  75. }
  76.  
  77. /*
  78. * 通过for each方法访问集合元素
  79. *
  80. * */
  81.  
  82. public void testForEach(){
  83. System.out.println("有如下课程待选(通过 for each访问):");
  84. for (Object object : CourseToSelect) {
  85. Course cr=(Course) object;
  86. System.out.println("课程:"+cr.id+":"+cr.name);
  87. }
  88. }
  89.  
  90. public static void main(String []args){
  91. ListTest list=new ListTest();
  92. list.addCourse();
  93. list.testGet();
  94. list.testIterator();
  95. list.testForEach();
  96. }
  97. }

运行结果为:

  • 学生选课——课程修改
  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course c=new Course("1","数据结构");
  23. CourseToSelect.add(c);
  24. Course temp=(Course) CourseToSelect.get(0);
  25. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  26.  
  27. Course c1=new Course("2","C语言");
  28. CourseToSelect.add(0, c1);
  29. Course temp1=(Course) CourseToSelect.get(0);
  30. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  31.  
  32. Course cq=new Course("1","数据结构");
  33. CourseToSelect.add(cq);
  34. Course temp0=(Course) CourseToSelect.get(2);
  35. System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
  36.  
  37. /*Course c2=new Course("2","Test1");
  38. CourseToSelect.add(4, c2);*/
  39. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  40. CourseToSelect.addAll(Arrays.asList(course));
  41. Course temp2=(Course) CourseToSelect.get(2);
  42. Course temp3=(Course) CourseToSelect.get(3);
  43. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  44.  
  45. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  46. CourseToSelect.addAll(2, Arrays.asList(course2));
  47. Course temp4=(Course) CourseToSelect.get(2);
  48. Course temp5=(Course) CourseToSelect.get(3);
  49. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  50.  
  51. }
  52. /*
  53. * 取得List中的元素的方法
  54. * */
  55. public void testGet(){
  56. System.out.println("有如下课程待选:");
  57. int size=CourseToSelect.size();
  58. for (int i = 0; i < size; i++) {
  59. Course cr=(Course) CourseToSelect.get(i);
  60. System.out.println("课程:"+cr.id+":"+cr.name);
  61. }
  62. }
  63.  
  64. /*
  65. * 通过迭代器来遍历List
  66. * */
  67. public void testIterator(){
  68. //通过集合iterator方法,取得迭代器实例
  69. Iterator it=CourseToSelect.iterator();
  70. System.out.println("有如下课程待选(通过迭代器访问):");
  71. while (it.hasNext()) {
  72. Course cr = (Course) it.next();
  73. System.out.println("课程:"+cr.id+":"+cr.name);
  74. }
  75. }
  76.  
  77. /*
  78. * 通过for each方法访问集合元素
  79. *
  80. * */
  81.  
  82. public void testForEach(){
  83. System.out.println("有如下课程待选(通过 for each访问):");
  84. for (Object object : CourseToSelect) {
  85. Course cr=(Course) object;
  86. System.out.println("课程:"+cr.id+":"+cr.name);
  87. }
  88. }
  89.  
  90. /*
  91. * 修改List中的元素
  92. * */
  93. public void testModify(){
  94. CourseToSelect.set(4,new Course("7","毛概"));
  95. }
  96.  
  97. public static void main(String []args){
  98. ListTest list=new ListTest();
  99. list.addCourse();
  100. list.testGet();
  101. list.testIterator();
  102. list.testForEach();
  103. list.testModify();
  104. list.testForEach();
  105. }
  106. }

运行结果为:

  • 学生选课——课程删除
  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course c=new Course("1","数据结构");
  23. CourseToSelect.add(c);
  24. Course temp=(Course) CourseToSelect.get(0);
  25. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  26.  
  27. Course c1=new Course("2","C语言");
  28. CourseToSelect.add(0, c1);
  29. Course temp1=(Course) CourseToSelect.get(0);
  30. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  31.  
  32. Course cq=new Course("1","数据结构");
  33. CourseToSelect.add(cq);
  34. Course temp0=(Course) CourseToSelect.get(2);
  35. System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
  36.  
  37. /*Course c2=new Course("2","Test1");
  38. CourseToSelect.add(4, c2);*/
  39. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  40. CourseToSelect.addAll(Arrays.asList(course));
  41. Course temp2=(Course) CourseToSelect.get(2);
  42. Course temp3=(Course) CourseToSelect.get(3);
  43. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  44.  
  45. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  46. CourseToSelect.addAll(2, Arrays.asList(course2));
  47. Course temp4=(Course) CourseToSelect.get(2);
  48. Course temp5=(Course) CourseToSelect.get(3);
  49. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  50.  
  51. }
  52. /*
  53. * 取得List中的元素的方法
  54. * */
  55. public void testGet(){
  56. System.out.println("有如下课程待选:");
  57. int size=CourseToSelect.size();
  58. for (int i = 0; i < size; i++) {
  59. Course cr=(Course) CourseToSelect.get(i);
  60. System.out.println("课程:"+cr.id+":"+cr.name);
  61. }
  62. }
  63.  
  64. /*
  65. * 通过迭代器来遍历List
  66. * */
  67. public void testIterator(){
  68. //通过集合iterator方法,取得迭代器实例
  69. Iterator it=CourseToSelect.iterator();
  70. System.out.println("有如下课程待选(通过迭代器访问):");
  71. while (it.hasNext()) {
  72. Course cr = (Course) it.next();
  73. System.out.println("课程:"+cr.id+":"+cr.name);
  74. }
  75. }
  76.  
  77. /*
  78. * 通过for each方法访问集合元素
  79. *
  80. * */
  81.  
  82. public void testForEach(){
  83. System.out.println("有如下课程待选(通过 for each访问):");
  84. for (Object object : CourseToSelect) {
  85. Course cr=(Course) object;
  86. System.out.println("课程:"+cr.id+":"+cr.name);
  87. }
  88. }
  89.  
  90. /*
  91. * 修改List中的元素
  92. * */
  93. public void testModify(){
  94. CourseToSelect.set(4,new Course("7","毛概"));
  95.  
  96. }
  97.  
  98. /*
  99. * 删除list中的元素
  100. * */
  101. public void testRemove(){
  102. Course cr=(Course) CourseToSelect.get(4);
  103. System.out.println("我是课程:"+cr.id+":"+cr.name+"我即将被要删除");
  104. CourseToSelect.remove(cr);
  105. System.out.println("成功删除课程");
  106. testForEach();
  107. }
  108.  
  109. public static void main(String []args){
  110. ListTest list=new ListTest();
  111. list.addCourse();
  112. list.testGet();
  113. list.testIterator();
  114. list.testForEach();
  115. list.testModify();
  116. list.testForEach();
  117. list.testRemove();
  118. }
  119. }

运行结果为:

  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. /*
  8. *备选课程类
  9. * */
  10. public class ListTest {
  11. /**
  12. * 用于存放备选课程List
  13. * */
  14. public List CourseToSelect;
  15. public ListTest(){
  16. this.CourseToSelect=new ArrayList();
  17.  
  18. }
  19. //用于往CourseToSelect中添加备选课程
  20. public void addCourse(){
  21. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  22. Course c=new Course("1","数据结构");
  23. CourseToSelect.add(c);
  24. Course temp=(Course) CourseToSelect.get(0);
  25. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  26.  
  27. Course c1=new Course("2","C语言");
  28. CourseToSelect.add(0, c1);
  29. Course temp1=(Course) CourseToSelect.get(0);
  30. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  31.  
  32. Course cq=new Course("1","数据结构");
  33. CourseToSelect.add(cq);
  34. Course temp0=(Course) CourseToSelect.get(2);
  35. System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
  36.  
  37. /*Course c2=new Course("2","Test1");
  38. CourseToSelect.add(4, c2);*/
  39. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  40. CourseToSelect.addAll(Arrays.asList(course));
  41. Course temp2=(Course) CourseToSelect.get(2);
  42. Course temp3=(Course) CourseToSelect.get(3);
  43. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  44.  
  45. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  46. CourseToSelect.addAll(2, Arrays.asList(course2));
  47. Course temp4=(Course) CourseToSelect.get(2);
  48. Course temp5=(Course) CourseToSelect.get(3);
  49. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  50.  
  51. }
  52. /*
  53. * 取得List中的元素的方法
  54. * */
  55. public void testGet(){
  56. System.out.println("有如下课程待选:");
  57. int size=CourseToSelect.size();
  58. for (int i = 0; i < size; i++) {
  59. Course cr=(Course) CourseToSelect.get(i);
  60. System.out.println("课程:"+cr.id+":"+cr.name);
  61. }
  62. }
  63.  
  64. /*
  65. * 通过迭代器来遍历List
  66. * */
  67. public void testIterator(){
  68. //通过集合iterator方法,取得迭代器实例
  69. Iterator it=CourseToSelect.iterator();
  70. System.out.println("有如下课程待选(通过迭代器访问):");
  71. while (it.hasNext()) {
  72. Course cr = (Course) it.next();
  73. System.out.println("课程:"+cr.id+":"+cr.name);
  74. }
  75. }
  76.  
  77. /*
  78. * 通过for each方法访问集合元素
  79. *
  80. * */
  81.  
  82. public void testForEach(){
  83. System.out.println("有如下课程待选(通过 for each访问):");
  84. for (Object object : CourseToSelect) {
  85. Course cr=(Course) object;
  86. System.out.println("课程:"+cr.id+":"+cr.name);
  87. }
  88. }
  89.  
  90. /*
  91. * 修改List中的元素
  92. * */
  93. public void testModify(){
  94. CourseToSelect.set(4,new Course("7","毛概"));
  95.  
  96. }
  97.  
  98. /*
  99. * 删除list中的多组元素
  100. * */
  101. public void testRemove(){
  102. System.out.println("即将删除4位置和5位置上的课程");
  103. Course[] course={(Course)CourseToSelect.get(4),(Course)CourseToSelect.get(5)};
  104. CourseToSelect.removeAll(Arrays.asList(course));
  105. System.out.println("成功删除课程");
  106. testForEach();
  107. }
  108.  
  109. public static void main(String []args){
  110. ListTest list=new ListTest();
  111. list.addCourse();
  112. list.testGet();
  113. list.testIterator();
  114. list.testForEach();
  115. list.testModify();
  116. list.testForEach();
  117. list.testRemove();
  118. }
  119. }

运行结果为:

  • 学生选课——应用泛型管理课程1

泛型

集合中的元素,可以是任意类型的对象(对象的引用)。如果把某一个对象放入集合,则会忽略他的类型,而把他当做Object处理

泛型则是规定了某个集合只可以存放特定类型的对象。会在编译期间进行类型检查,可以直接按指定类型获取集合元素

  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class TestGeneric {
  7. //带有泛型Course的List类型属性
  8. public List<Course>courses;
  9. public TestGeneric(){
  10. this.courses=new ArrayList<Course>();
  11. }
  12.  
  13. //测试
  14. public void testAdd(){
  15. Course cr1=new Course("1","大学英语");
  16. courses.add(cr1);
  17. Course cr2=new Course("2","Java基础");
  18. courses.add(cr2);
  19. //泛型集合中,不能添加泛型规定的类型以外的对象,否则会报错!
  20. //courses.add("可以添加奇怪的东西吗??");
  21. }
  22. //测试循环遍历
  23. public void testForEach(){
  24. for (Course cr:courses) {
  25. System.out.println(cr.id+":"+cr.name);
  26. }
  27. }
  28. /**
  29. * @param args
  30. */
  31. public static void main(String[] args) {
  32. // TODO Auto-generated method stub
  33. TestGeneric tg=new TestGeneric();
  34. tg.testAdd();
  35. tg.testForEach();
  36. }
  37.  
  38. }

  • 学生选课——应用泛型管理课程11

1.泛型集合中的限定类型不能使用基本数据类型。

2.可以通过使用包装类限定允许存入的基本数据类型。

  •  学生选课——通过Set集合管理课

Set接口及其实现类——HashSet

  1.set是元素无序并且不可以重复的集合,被称为集。

  2.HashSet——哈希集,是Set的一个重要实现类

  1. package com.imooc.collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. public class SetTest {
  10.  
  11. public List<Course>coursesToSelect;
  12. public SetTest(){
  13. coursesToSelect=new ArrayList<Course>();
  14. }
  15.  
  16. //用于往CourseToSelect中添加备选课程
  17. public void testAdd(){
  18. //创建一个课程对象,并通过调用add方法,添加到备选课程List中
  19. Course c=new Course("1","数据结构");
  20. coursesToSelect.add(c);
  21. Course temp=(Course) coursesToSelect.get(0);
  22. System.out.println("添加了一门课程"+temp.id+":"+temp.name);
  23.  
  24. Course c1=new Course("2","C语言");
  25. coursesToSelect.add(0, c1);
  26. Course temp1=(Course) coursesToSelect.get(0);
  27. System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
  28.  
  29. Course cq=new Course("1","数据结构");
  30. coursesToSelect.add(cq);
  31. Course temp0=(Course) coursesToSelect.get(2);
  32. System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
  33.  
  34. /*Course c2=new Course("2","Test1");
  35. CourseToSelect.add(4, c2);*/
  36. Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
  37. coursesToSelect.addAll(Arrays.asList(course));
  38. Course temp2=(Course) coursesToSelect.get(2);
  39. Course temp3=(Course) coursesToSelect.get(3);
  40. System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
  41.  
  42. Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
  43. coursesToSelect.addAll(2, Arrays.asList(course2));
  44. Course temp4=(Course) coursesToSelect.get(2);
  45. Course temp5=(Course) coursesToSelect.get(3);
  46. System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
  47.  
  48. }
  49.  
  50. /* * 取得List中的元素的方法
  51. * */
  52. public void testGet(){
  53. System.out.println("有如下课程待选:");
  54. int size=coursesToSelect.size();
  55. for (int i = 0; i < size; i++) {
  56. Course cr=(Course) coursesToSelect.get(i);
  57. System.out.println("课程:"+cr.id+":"+cr.name);
  58. }
  59. }
  60.  
  61. /* * 通过迭代器来遍历List
  62. * */
  63. public void testIterator(){
  64. //通过集合iterator方法,取得迭代器实例
  65. Iterator it=coursesToSelect.iterator();
  66. System.out.println("有如下课程待选(通过迭代器访问):");
  67. while (it.hasNext()) {
  68. Course cr = (Course) it.next();
  69. System.out.println("课程:"+cr.id+":"+cr.name);
  70. }
  71. }
  72.  
  73. /** 通过for each方法访问集合元素
  74. *
  75. * */
  76.  
  77. public void testForEach(){
  78. System.out.println("有如下课程待选(通过 for each访问):");
  79. for (Object object : coursesToSelect) {
  80. Course cr=(Course) object;
  81. System.out.println("课程:"+cr.id+":"+cr.name);
  82. }
  83. }
  84.  
  85. /**
  86. * @param argsS
  87. */
  88. public static void main(String[] args) {
  89. // TODO Auto-generated method stub
  90. SetTest st=new SetTest();
  91. st.testAdd();
  92. st.testForEach();
  93. //创建一个学生对象
  94. Student student=new Student("1","小明");
  95. System.out.println("欢迎学生"+student.name+"选课");
  96. //创建一个Scanner对象,用来接收从键盘输入的课程ID
  97. Scanner console=new Scanner(System.in);
  98. for (int i = 0; i < 3; i++) {
  99. System.out.println("请输入课程ID");
  100. String courseId=console.next();
  101. for (Course cr : st.coursesToSelect) {
  102. if (cr.id.equals(courseId)) {
  103. student.courses.add(cr);
  104. /*Set中添加某个对象,无论添加杜少次,
  105. * 最终只会保留一个该对象(的引用)
  106. * 并且,保留的是第一次添加的那一个*/
  107. //student.courses.add(null);
  108. //student.courses.add(cr);
  109. }
  110. }
  111. }
  112. st.testForEachForSet(student);
  113. }
  114.  
  115. public void testForEachForSet(Student student){
  116. //打印输出,学生所选的课程
  117. System.out.println("共选择了"+student.courses.size()+"门课程!");
  118. for(Course cr:student.courses){
  119. System.out.println("选择了课程"+cr.id+":"+"cr.name");
  120. }
  121.  
  122. }
  123. }

运行结果为:

Java中的集合框架(上)的更多相关文章

  1. 菜鸟日记之 java中的集合框架

    java中的集合框架图 如图所示:java中的集合分为两种Collection和Map两种接口 可分为Collection是单列集合和Map的双列集合 Collection单列集合:继承了Iterat ...

  2. Java中的集合框架-Collections和Arrays

    上一篇<Java中的集合框架-Map>把集合框架中的键值对容器Map中常用的知识记录了一下,本节记录一下集合框架的两个工具类Collections和Arrays 一,Collections ...

  3. Java中的集合框架-Map

    前两篇<Java中的集合框架-Commection(一)>和<Java中的集合框架-Commection(二)>把集合框架中的Collection开发常用知识点作了一下记录,从 ...

  4. Java中的集合框架-Collection(二)

    上一篇<Java中的集合框架-Collection(一)>把Java集合框架中的Collection与List及其常用实现类的功能大致记录了一下,本篇接着记录Collection的另一个子 ...

  5. Java中的集合框架

    概念与作用 集合概念 现实生活中:很多事物凑在一起 数学中的集合:具有共同属性的事物的总体 java中的集合类:是一种工具类,就像是容器,储存任意数量的具有共同属性的对象 在编程时,常常需要集中存放多 ...

  6. Java中的集合框架(下)

    学生选课--判断Set中课程是否存在 package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays ...

  7. Java中的集合框架-Collection(一)

    一,Collection接口 在日常的开发工作中,我们经常使用数组,但是数组是有很多的局限性的,比如:数组大小固定后不可修改,只能存储基本类型的值等等. 基于数组的这些局限性,Java框架就产生了用于 ...

  8. JAVA 中的集合框架

    java集合框架提供了一套性能优良.使用方便的接口和类,它们位于java.util包中 一.集合与数组 数组:(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知 ...

  9. Java学习--java中的集合框架、Collection接口、list接口

    与数组相比:1.数组的长度固定,而集合的长度可变2.数组只能通过下表访问元素,类型固定,而有的集合可以通过任意类型查找所映射的具体对象 java集合框架:collection(list序列,queue ...

随机推荐

  1. 迭代var()内置函数的时候出现RuntimeError: dictionary changed size during iteration的解决办法

    下午看了Mr Seven的教学视频,其中有一段讲全局变量的视频,迭代输出全局变量的时候报错了. 视频中的做法: for k,v in vars().items(): print(k) 打印结果 for ...

  2. redis主从配置+哨兵模式

    1.搭建redis主从,一个master两个slave,加一个哨兵监听(sentinel),可以新建三个虚拟机,模拟环境,我的电脑没那么多虚拟机,就在一台虚拟机上弄的. 2.安装redis,如果是三台 ...

  3. .Net 关于 InfoPath 的基本使用

    最近几天接触微软的  InfoPath 表单工具,结合VS 使用,遇到的一些问题与解决方法,记录一下,百度上的内容很少,或许我根本不知道咋搜,所有也留一个网页帮助自己使用当然使用的时候碰到了好几个坑: ...

  4. 《程序设计语言——实践之路【PDF】下载

    <程序设计语言--实践之路[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382240 内容简介 <程序设计语言--实践之路(第3版 ...

  5. 【转】Android开发之数据库SQL

    android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库 android中采用的数据库是SQLi ...

  6. 小白的Python之路 day4 装饰器前奏

    装饰器前奏: 一.定义: 1.装饰器本质是函数,语法都是用def去定义的 (函数的目的:他需要完成特定的功能) 2.装饰器的功能:就是装饰其他函数(就是为其他函数添加附加功能) 二.原则: 1. 不能 ...

  7. OC学习15——文件I/O体系

    OC提供了丰富的I/O相关API,如果只是管理文件和目录,程序可以使用NSFileManager进行管理,包括创建.删除.移动和复制文件等:如果程序需要读取文件内容,则可通过NSFileHandle进 ...

  8. 前端开发必备之MDN文档

    想下载MDN文档的看前面的内容就可以了. HTML 源码下载 MDN官方下载地址:https://developer.mozilla.org/media/developer.mozilla.org.t ...

  9. iOS 面试题、知识点 之一

    最近面试,发现这些题个人遇到的几率大一些,与大家分享一下,分三文给大家: 当然Xcode新版本与之前一版本的区别,以及iOS新特性是必要了解的吧. Xcode8 和iOS 10 在之前文章有发过,感兴 ...

  10. bzoj 1996: [Hnoi2010]chorus 合唱队

    Description Input Output Sample Input 4 1701 1702 1703 1704 Sample Output 8 HINT Source 因为只会在区间的两端进行 ...