摘要:中的集合框架目录结构创建学生类和课程类课程类课程类学生类学生类添加课程添加的方法用于存放备选课程的用于往中添加备选课程创建一个课程对象,并通过调用方法,添加到备选课程中数据结构添加了课程语言添加了课程数组下标越界异常用于存放备选课程的用于往
java中的集合框架 目录结构 创建学生类和课程类 课程类
/imooc_collection_map_demo/src/com/imooc/collection/Course.java
package com.imooc.collection; //课程类 public class Course { public String id; public String name; public Course(String id, String name) { this.id = id; this.name = name; } }学生类
/imooc_collection_map_demo/src/com/imooc/collection/Student.java
package com.imooc.collection; //学生类 import java.util.HashSet; import java.util.Set; public class Student { public String id; public String name; public Set courses; public Student(String id ,String name) { this.id = id; this.name = name; this.courses = new HashSet(); } }添加课程 添加的方法
/imooc_collection_map_demo/src/com/imooc/collection/ListTest.java
package com.imooc.collection; import java.util.ArrayList; import java.util.List; public class ListTest { //用于存放备选课程的List public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //用于往coursesToSelect中添加备选课程 public void testAdd() { //创建一个课程对象,并通过调用add方法,添加到备选课程list中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp.id + ":" + temp.name); Course cr2 = new Course("2", "c语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); } public static void main(String[] args) { ListTest lt = new ListTest(); lt.testAdd(); } }数组下标越界异常
/imooc_collection_map_demo/src/com/imooc/collection/ListTest.java
package com.imooc.collection; import java.util.ArrayList; import java.util.List; public class ListTest { //用于存放备选课程的List public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //用于往coursesToSelect中添加备选课程 public void testAdd() { //创建一个课程对象,并通过调用add方法,添加到备选课程list中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp.id + ":" + temp.name); Course cr2 = new Course("2", "c语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); Course cr3 = new Course("3", "test"); coursesToSelect.add(-1,cr3); Course cr3 = new Course("3", "test"); coursesToSelect.add(4,cr3); } public static void main(String[] args) { ListTest lt = new ListTest(); lt.testAdd(); } }数组形式添加
/imooc_collection_map_demo/src/com/imooc/collection/ListTest.java
package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListTest { //用于存放备选课程的List public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //用于往coursesToSelect中添加备选课程 public void testAdd() { //创建一个课程对象,并通过调用add方法,添加到备选课程list中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp.id + ":" + temp.name); Course cr2 = new Course("2", "c语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); // Course cr3 = new Course("3", "test"); // coursesToSelect.add(-1,cr3); Course[] course = {new Course("3", "离散数学"),new Course("4", "汇编语言")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp3.id + ":" + temp3.name+ temp4.id + ":" + temp4.name); Course[] course2 = {new Course("5", "高等数学"),new Course("6", "大学英语")}; coursesToSelect.addAll(2,Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp5.id + ":" + temp5.name+ temp6.id + ":" + temp6.name); } public static void main(String[] args) { ListTest lt = new ListTest(); lt.testAdd(); } }查询课程
/imooc_collection_map_demo/src/com/imooc/collection/ListTest.java查询代码
//取得list中的元素的方法 public void testGet() { int size = coursesToSelect.size(); System.out.println("有如下课程待选1"); for(int i = 0;i完整代码 package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import sun.security.krb5.internal.crypto.crc32; public class ListTest { //用于存放备选课程的List public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //用于往coursesToSelect中添加备选课程 public void testAdd() { //创建一个课程对象,并通过调用add方法,添加到备选课程list中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp.id + ":" + temp.name); Course cr2 = new Course("2", "c语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); // Course cr3 = new Course("3", "test"); // coursesToSelect.add(-1,cr3); Course[] course = {new Course("3", "离散数学"),new Course("4", "汇编语言")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp3.id + ":" + temp3.name+ temp4.id + ":" + temp4.name); Course[] course2 = {new Course("5", "高等数学"),new Course("6", "大学英语")}; coursesToSelect.addAll(2,Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp5.id + ":" + temp5.name+ temp6.id + ":" + temp6.name); } //取得list中的元素的方法 public void testGet() { int size = coursesToSelect.size(); System.out.println("有如下课程待选1"); for(int i = 0;i打印 修改课程 修改部分代码 //修改list中的元素 public void testModify() { coursesToSelect.set(4, new Course("7","毛概")); }全部代码package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import com.sun.org.apache.bcel.internal.generic.NEW; import sun.security.krb5.internal.crypto.crc32; public class ListTest { //用于存放备选课程的List public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //用于往coursesToSelect中添加备选课程 public void testAdd() { //创建一个课程对象,并通过调用add方法,添加到备选课程list中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp.id + ":" + temp.name); Course cr2 = new Course("2", "c语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); // Course cr3 = new Course("3", "test"); // coursesToSelect.add(-1,cr3); Course[] course = {new Course("3", "离散数学"),new Course("4", "汇编语言")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp3.id + ":" + temp3.name+ temp4.id + ":" + temp4.name); Course[] course2 = {new Course("5", "高等数学"),new Course("6", "大学英语")}; coursesToSelect.addAll(2,Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp5.id + ":" + temp5.name+ temp6.id + ":" + temp6.name); } //取得list中的元素的方法 public void testGet() { int size = coursesToSelect.size(); System.out.println("有如下课程待选1"); for(int i = 0;i打印 删除课程 删除部分代码 //删除list中的元素 public void testRemove() { // 1.通过key删除 // Course cr = (Course) coursesToSelect.get(4); // System.out.println("我是课程:" + cr.id + ":" + cr.name + "我即将被删除"); // coursesToSelect.remove(cr); // 2.通过index删除 // System.out.println("删除4位置上的课程"); // coursesToSelect.remove(4); // 3.删除数组 System.out.println("删除4和5位置上的课程"); Course[] courses = {(Course) coursesToSelect.get(4),(Course) coursesToSelect.get(5)}; coursesToSelect.removeAll(Arrays.asList(courses)); System.out.println("成功删除课程"); testForEach(); }全部代码package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import com.sun.org.apache.bcel.internal.generic.NEW; import sun.security.krb5.internal.crypto.crc32; public class ListTest { //用于存放备选课程的List public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //用于往coursesToSelect中添加备选课程 public void testAdd() { //创建一个课程对象,并通过调用add方法,添加到备选课程list中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp.id + ":" + temp.name); Course cr2 = new Course("2", "c语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); // Course cr3 = new Course("3", "test"); // coursesToSelect.add(-1,cr3); Course[] course = {new Course("3", "离散数学"),new Course("4", "汇编语言")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp3.id + ":" + temp3.name+ temp4.id + ":" + temp4.name); Course[] course2 = {new Course("5", "高等数学"),new Course("6", "大学英语")}; coursesToSelect.addAll(2,Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); System.out.println("添加了课程:" + temp5.id + ":" + temp5.name+ temp6.id + ":" + temp6.name); } //取得list中的元素的方法 public void testGet() { int size = coursesToSelect.size(); System.out.println("有如下课程待选1"); for(int i = 0;i打印
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/73189.html
摘要:泛型增加不同于课程类型的数据往里增加字符串字符串泛型实现带有泛型的类型属性添加大学语文基础循环遍历泛型子类型红色报错解决办法添加无参的构造方法泛型集合可以添加泛型的子类型的对象实例我是子类型的课程对象实例泛型不能使用基本类型基 泛型 showImg(https://segmentfault.com/img/bVbnR10?w=1085&h=559); showImg(https://s...
摘要:编程思想第版这本书要常读,初学者可以快速概览,中等程序员可以深入看看,老鸟还可以用之回顾的体系。以下视频整理自慕课网工程师路径相关免费课程。 我自己总结的Java学习的系统知识点以及面试问题,目前已经开源,会一直完善下去,欢迎建议和指导欢迎Star: https://github.com/Snailclimb/Java-Guide 笔者建议初学者学习Java的方式:看书+视频+实践(初...
摘要:集合中的集合是一种工具类,就像是容器,存储任意数量的具有共同属性的对象集合的作用在类的内部,对数据进行组织简单而快速的搜索大量数目的条目有的集合接口,提供了一系列排列有序的元素,并且可以在序列中进行快速的插入和删除有些集合接口,提供了映射关 集合 java中的集合: 是一种工具类,就像是容器,存储任意数量的具有共同属性的对象 集合的作用 1. 在类的内部,对数据进行组织 2. 简单而快...
阅读 2413·2021-11-19 09:40
阅读 3578·2021-10-12 10:12
阅读 1885·2021-09-22 15:04
阅读 2901·2021-09-02 09:53
阅读 763·2019-08-29 11:03
阅读 1124·2019-08-28 18:11
阅读 1726·2019-08-23 15:28
阅读 3582·2019-08-23 15:05