摘要:中的集合框架目錄結(jié)構(gòu)創(chuàng)建學(xué)生類和課程類課程類課程類學(xué)生類學(xué)生類添加課程添加的方法用于存放備選課程的用于往中添加備選課程創(chuàng)建一個課程對象,并通過調(diào)用方法,添加到備選課程中數(shù)據(jù)結(jié)構(gòu)添加了課程語言添加了課程數(shù)組下標(biāo)越界異常用于存放備選課程的用于往
java中的集合框架 目錄結(jié)構(gòu) 創(chuàng)建學(xué)生類和課程類 課程類
/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; } }學(xué)生類
/imooc_collection_map_demo/src/com/imooc/collection/Student.java
package com.imooc.collection; //學(xué)生類 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() { //創(chuàng)建一個課程對象,并通過調(diào)用add方法,添加到備選課程list中 Course cr1 = new Course("1", "數(shù)據(jù)結(jié)構(gòu)"); 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(); } }數(shù)組下標(biāo)越界異常
/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() { //創(chuàng)建一個課程對象,并通過調(diào)用add方法,添加到備選課程list中 Course cr1 = new Course("1", "數(shù)據(jù)結(jié)構(gòu)"); 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(); } }數(shù)組形式添加
/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() { //創(chuàng)建一個課程對象,并通過調(diào)用add方法,添加到備選課程list中 Course cr1 = new Course("1", "數(shù)據(jù)結(jié)構(gòu)"); 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", "離散數(shù)學(xué)"),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", "高等數(shù)學(xué)"),new Course("6", "大學(xué)英語")}; 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() { //創(chuàng)建一個課程對象,并通過調(diào)用add方法,添加到備選課程list中 Course cr1 = new Course("1", "數(shù)據(jù)結(jié)構(gòu)"); 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", "離散數(shù)學(xué)"),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", "高等數(shù)學(xué)"),new Course("6", "大學(xué)英語")}; 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() { //創(chuàng)建一個課程對象,并通過調(diào)用add方法,添加到備選課程list中 Course cr1 = new Course("1", "數(shù)據(jù)結(jié)構(gòu)"); 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", "離散數(shù)學(xué)"),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", "高等數(shù)學(xué)"),new Course("6", "大學(xué)英語")}; 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.刪除數(shù)組 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() { //創(chuàng)建一個課程對象,并通過調(diào)用add方法,添加到備選課程list中 Course cr1 = new Course("1", "數(shù)據(jù)結(jié)構(gòu)"); 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", "離散數(shù)學(xué)"),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", "高等數(shù)學(xué)"),new Course("6", "大學(xué)英語")}; 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打印
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73189.html
摘要:泛型增加不同于課程類型的數(shù)據(jù)往里增加字符串字符串泛型實(shí)現(xiàn)帶有泛型的類型屬性添加大學(xué)語文基礎(chǔ)循環(huán)遍歷泛型子類型紅色報(bào)錯解決辦法添加無參的構(gòu)造方法泛型集合可以添加泛型的子類型的對象實(shí)例我是子類型的課程對象實(shí)例泛型不能使用基本類型基 泛型 showImg(https://segmentfault.com/img/bVbnR10?w=1085&h=559); showImg(https://s...
摘要:編程思想第版這本書要常讀,初學(xué)者可以快速概覽,中等程序員可以深入看看,老鳥還可以用之回顧的體系。以下視頻整理自慕課網(wǎng)工程師路徑相關(guān)免費(fèi)課程。 我自己總結(jié)的Java學(xué)習(xí)的系統(tǒng)知識點(diǎn)以及面試問題,目前已經(jīng)開源,會一直完善下去,歡迎建議和指導(dǎo)歡迎Star: https://github.com/Snailclimb/Java-Guide 筆者建議初學(xué)者學(xué)習(xí)Java的方式:看書+視頻+實(shí)踐(初...
摘要:集合中的集合是一種工具類,就像是容器,存儲任意數(shù)量的具有共同屬性的對象集合的作用在類的內(nèi)部,對數(shù)據(jù)進(jìn)行組織簡單而快速的搜索大量數(shù)目的條目有的集合接口,提供了一系列排列有序的元素,并且可以在序列中進(jìn)行快速的插入和刪除有些集合接口,提供了映射關(guān) 集合 java中的集合: 是一種工具類,就像是容器,存儲任意數(shù)量的具有共同屬性的對象 集合的作用 1. 在類的內(nèi)部,對數(shù)據(jù)進(jìn)行組織 2. 簡單而快...
閱讀 2419·2021-11-19 09:40
閱讀 3588·2021-10-12 10:12
閱讀 1897·2021-09-22 15:04
閱讀 2910·2021-09-02 09:53
閱讀 775·2019-08-29 11:03
閱讀 1130·2019-08-28 18:11
閱讀 1734·2019-08-23 15:28
閱讀 3588·2019-08-23 15:05