当前位置:网站首页>Set集合
Set集合
2022-08-09 07:56:00 【洛央虲】
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course> coursesToSelect;
public SetTest(){
coursesToSelect = new ArrayList<Course>();
}
/** * 用于往coursesToSelect中添加备选课程 */
public void testAdd(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course cr1 = new Course("1","数据结构");
coursesToSelect.add(cr1);
//取出元素 get方法
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);
coursesToSelect.add(cr1);
//取出元素 get方法
Course temp0 = (Course)coursesToSelect.get(2);
//System.out.println("添加了课程:"+temp.id+":"+temp.name);
//以下方法会抛出数组下标越界异常
//Course cr3 = new Course("3","Test");
//coursesToSelect.add(4,cr3);数组越界
Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp3 = (Course)coursesToSelect.get(3);
Course temp4 = (Course)coursesToSelect.get(4);
//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);
}
/** * 通过for each方法访问集合元素 * @param args */
public void testForEach(){
System.out.println("有如下课程待选(通过for each访问):");
for(Object obj:coursesToSelect){
Course cr = (Course)obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SetTest st = new SetTest();
st.testAdd();
st.testForEach();
//创建一个学生对象
Student student = new Student("1","小王");
System.out.println("欢迎学生:"+student.name+"选课!");
//创建要给Scanner对象,用来接收从键盘输入的课程ID
Scanner console = new Scanner(System.in);
for(int i=0;i<3;i++){
System.out.println("请输入课程ID");
String courseId = console.next();
for(Course cr:st.coursesToSelect){
if(cr.id.equals(courseId)){
student.courses.add(cr);
/** * Set中,添加某个对象,无论添加多少次 * 最终只会保留一个该对象(的引用), * 并且,保留第一次添加的那一个 */
//student.courses.add(cr);
//student.courses.add(null);
}
}
}
st.testForEachForSet(student);
}
public void testForEachForSet(Student student){
//打印输出,学生所选的课程!
System.out.println("供选择了:"+student.courses.size());
for(Course cr:student.courses){
System.out.println("选择了课程:"+cr.id+":"+cr.name);
}
}
}
边栏推荐
- SA-Siam:用于实时目标跟踪的双重连体网络A Twofold Siamese Network for Real-Time Object Tracking
- Web 3D渲染引擎HOOPS Communicator动画编辑器的使用 | HOOPS教程
- Data storage implementation of SDRAM and read and write operations on its data
- ssh:connect to host master port 22:Network is unreachable
- (error) NOAUTH Authentication required.
- Solidworks 2022 Inspection新增功能:光学字符识别、可自定义的检查报告
- LeetCode: 876. The middle node of the linked list —— simple
- pip安装更换镜像
- (五)、马尔科夫预测模型
- .net(一)WebService创建
猜你喜欢
Shell--常用小工具(sort、uniq、tr、cut)
链表专项练习(四)
3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Visualize、HOOPS Publish
c语言位段
C language: adjust the order of odd and even numbers
LVM与磁盘配额
ImportError: cannot import name ‘imresize‘
SSM整合开发案例
The String class objects created by the JVM memory allocation and the difference between equals and = =
低成本、大容量、高交互…Polkadot 引领 GameFi 实现新突破
随机推荐
Colors that Tkinter can choose from
tianqf's problem-solving ideas
【Template】Tree Chain Segmentation P3384
权限(下)
HOOPS助力 SolidWorks edrawings 引入AR/VR技术
SDRAM的数据存储实现并对其数据进行读写操作
传输层协议介绍
三层交换机原理及配置
记录一次客户的APP数据库版本号升级失败的情况
VOC format label to YOLO format
Anaconda use proxy
Use tensorflow.keras to build a neural network model modularly
Invoker 2019CCPC Qinhuangdao Station I Question Simple DP
LeetCode·每日一题·636.函数的独占时间·栈模拟
BIM技术多牛逼?BIM技术在建筑工程行业的四大发展趋势
[STL]string
【机器学习】降维代码练习
C语言:调整奇数偶数顺序
Four departments including the Ministry of Industry and Information Technology promote green smart home products to the countryside
Native JDBC operation database