摘要:中的集合框架目錄結構創建學生類和課程類課程類課程類學生類學生類添加課程添加的方法用于存放備選課程的用于往中添加備選課程創建一個課程對象,并通過調用方法,添加到備選課程中數據結構添加了課程語言添加了課程數組下標越界異常用于存放備選課程的用于往
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打印
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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. 簡單而快...
閱讀 2410·2021-11-19 09:40
閱讀 3575·2021-10-12 10:12
閱讀 1884·2021-09-22 15:04
閱讀 2898·2021-09-02 09:53
閱讀 762·2019-08-29 11:03
閱讀 1122·2019-08-28 18:11
閱讀 1724·2019-08-23 15:28
閱讀 3580·2019-08-23 15:05