当前位置:网站首页>Contains方法,查看序列中是否包含某个元素
Contains方法,查看序列中是否包含某个元素
2022-07-22 18:10:00 【洛央虲】
Equals方法重写
package com.imooc.collection;
/** * 课程类 * @author Monica * */
public class Course {
public String id;
public String name;
public Course(String id,String name){
this.id = id;
this.name = name;
}
public Course(){
}
@Override
public boolean equals(Object obj){
if(this == obj){
//通过双等号判断对象相等
return true;
}
if(obj == null)//如果obj等于空
return false;
if(!(obj instanceof Course))//ogj对象不是Course类型
return false;
//前面都通过了
Course course = (Course)obj;//强转为course对象
if(this.name == null){
//通过名称判断是否为course对象,判断名称是否为空
if(course.name == null)//强转为course的对象是否为空,两个都为空,是相等的
return true;
else
return false;
}else{
//当前的对象不为空
if(this.name.equals(course.name))//判断name是否相等
return true;
else//name不相等
return false;
}
}
}
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;
private Scanner console;
public SetTest(){
coursesToSelect = new ArrayList<Course>();
console = new Scanner(System.in);
}
/** * 用于往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);
}
}
/** * 测试List的contains方法 * containsAll方法,是否包含多个元素 * @param args */
public void testListContains(){
//取得备选课程序列的第0个元素
Course course = coursesToSelect.get(0);
//打印输出courseToSelected是否包含course对象
System.out.println("取得课程:"+course.name);
System.out.println("备选课程中是否包含课程:"+course.name+","+coursesToSelect.contains(course));
//提示输入课程名称
System.out.println("请输入课程名称:");
String name = console.next();
//创建一个新的课程对象,ID和名称,与course对象完全一样
Course course2 = new Course();
course2.name = name;
System.out.println("新创建课程:"+course2.name);
System.out.println("备选课程中是否包含课程:"+course2.name+","+coursesToSelect.contains(course2));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SetTest st = new SetTest();
st.testAdd();
st.testListContains();
/*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);
}
}
}
边栏推荐
- 测试软件开发---软件缺陷章
- Day 2 summary and test case operation
- 常用测试用例方法
- 移動端測試之appium環境部署【未完待續】
- OpenGL create a new window
- 关系型数据库10 分钟了解 MySQL
- LINK : fatal error LNK1104: 无法打开文件“opencv_world340.lib”
- Divide according to whether to check the original code: white box test and black box test
- Structure and development layering of games104 b1+b2 engine
- Software test classification
猜你喜欢
随机推荐
Automated testing - technology sharing
Test case: QQ login
Divided by whether to run source code: static test and dynamic test
amber教程3.2:GPU查看和用pmemd引擎跑MD
性能测试流程
Selenium基础知识 控制浏览器的滚动条
Building wheel for hdbscan (pyproject.toml) did not run successfully.
jmeter压力测试指标解释
面向腾讯(实战)-测试开发-实习生(面经)详解
selenium使用
BeanShell内置变量prev的使用技巧
读《机器学习-周志华》内容摘要
À propos du montage de fond, de la gestion des processus
Appium environment deployment for mobile terminal testing [unfinished to be continued]
用jmeter对抽奖接口进行抽奖概率分析测试
Solutions to xftp upload errors
Test software development - Software Defect chapter
Day 2 summary and test case operation
Link: fatal error lnk1104: unable to open the file "opencv_world340.lib"
How does app automated testing achieve H5 testing









