成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

java入門第三季--java中的集合框架(list)--增刪改查

OldPanda / 2367人閱讀

摘要:中的集合框架目錄結(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

相關(guān)文章

  • java門第三季--java中的集合框架list)--泛型

    摘要:泛型增加不同于課程類型的數(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...

    LancerComet 評論0 收藏0
  • 一份送給Java初學(xué)者的指南

    摘要:編程思想第版這本書要常讀,初學(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í)踐(初...

    banana_pi 評論0 收藏0
  • JAVA學(xué)習(xí)之路 (十)集合

    摘要:集合中的集合是一種工具類,就像是容器,存儲任意數(shù)量的具有共同屬性的對象集合的作用在類的內(nèi)部,對數(shù)據(jù)進(jìn)行組織簡單而快速的搜索大量數(shù)目的條目有的集合接口,提供了一系列排列有序的元素,并且可以在序列中進(jìn)行快速的插入和刪除有些集合接口,提供了映射關(guān) 集合 java中的集合: 是一種工具類,就像是容器,存儲任意數(shù)量的具有共同屬性的對象 集合的作用 1. 在類的內(nèi)部,對數(shù)據(jù)進(jìn)行組織 2. 簡單而快...

    sutaking 評論0 收藏0
  • java中的集合

    摘要:中的集合類是一種工具類,就像一種容器,存儲任意數(shù)量的具有共同屬性的對象。中集合框架體系結(jié)構(gòu)接口,子接口以及實(shí)現(xiàn)類接口是的父接口定義了可用于操作的方法增刪改查接口及其實(shí)現(xiàn)類是元素有序并且可以重復(fù)的集合,被稱為序列。 java中的集合類:是一種工具類,就像一種容器,存儲任意數(shù)量的具有共同屬性的對象。 showImg(https://segmentfault.com/img/bVXnWe?w...

    paney129 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<