Collection接口
??for???
发布日期:2021-04-30 21:06:11
浏览次数:96
分类:精选文章
本文共 3716 字,大约阅读时间需要 12 分钟。
Java ????
?????
??????????????????????????????
- ?????????????
- ??????????????
- ???????????????
???Java????????????????????????????????
???????
??????????????????
- ?????
boolean add(Object obj)??????????? - ???????
boolean addAll(Collection c)??????????????????? - ?????
void clear()?????????? - ???????
boolean contains(Object o)????????????? - ?????
boolean equals(Object o)?????????????? - ????
boolean isEmpty()???????? - ?????
boolean remove(Object o)?????????? - ???????
int size()?????????? - ??????
Object[] toArray()????????
Java???????
???????
// ????Collection collection = new ArrayList();// ????collection.add(new Student("??", 20));collection.add(new Student("??", 21));collection.add(new Student("??", 22));// ??????System.out.println("?????" + collection.size()); // ???3 ???????
???????????????
// ????????for (Object obj : collection) { Student stu = (Student) obj; System.out.println(stu.toString());} - ????
// ?????Iterator it = collection.iterator();// ????while (it.hasNext()) { Student s = (Student) it.next(); System.out.println(s.toString());} ??????????
// ??????????System.out.println(collection.contains(new Student("??", 21))); // ???trueSystem.out.println(collection.isEmpty()); // ???false ???????
// ??????collection.remove(new Student("??", 21));System.out.println("?????" + collection.size()); // ???1 ??????????
?????????????????????????????????
???Student?
public class Student { private String name; private int age; public Student(String name, int age) { super(); this.name = name; this.age = age; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } @Override public boolean equals(Object obj) { // ?????????? if (this == obj) { return true; } // ???????? if (obj == null) { return false; } // ?????? if (obj instanceof Student) { Student s = (Student) obj; // ???? return this.name.equals(s.getName()) && this.age == s.getAge(); } return false; }} ???????
public class Test { public static void main(String[] args) { // ???? Collection collection = new ArrayList(); // ?????? Student s1 = new Student("??", 20); Student s2 = new Student("??", 21); Student s3 = new Student("??", 22); collection.add(s1); collection.add(s2); collection.add(s3); System.out.println("?????" + collection.size()); // ???3 // ???????? for (Object obj : collection) { Student stu = (Student) obj; System.out.println(stu.toString()); } // ??????? Iterator it = collection.iterator(); while (it.hasNext()) { Student s = (Student) it.next(); System.out.println(s.toString()); } // ???????????? System.out.println(collection.contains(s1)); // ???true System.out.println(collection.contains(new Student("??", 21))); // ???true // ???????? System.out.println(collection.isEmpty()); // ???false // ???? collection.remove(s1); System.out.println("?????" + collection.size()); // ???2 collection.remove(new Student("??", 21)); System.out.println("?????" + collection.size()); // ???1 }} 发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2026年06月10日 04时19分22秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php英语单词,php常用英语单词,快速学习php编程英语(6)
2023-03-01
PHP获取curl传输进度
2023-03-01
PHP获取IP所在地区(转)
2023-03-01
PHP获取IP的方法对比
2023-03-01
php获取json里面内容
2023-03-01
R2的版本由来
2023-03-01
PHP获取图片宽度高度、大小尺寸、图片类型、用于布局的img属性
2023-03-01
PHP获取当前文件的绝对路径
2023-03-01
PHP获取当前时间、时间戳的各种格式写法汇总
2023-03-01
PHP获取当前页面的完整URL
2023-03-01
php获取数据库中数据生成json,中文乱码问题的解决方案
2023-03-01
php获取文件夹中文件的两种方法
2023-03-01
PHP获取日期的一些方法总结
2023-03-01
R2学习记录
2023-03-01
PHP获取本周的每一天的时间
2023-03-01
php获取用户真实IP和防刷机制
2023-03-01
php获取网页内容的三种方法
2023-03-01
R-CNN算法优化策略
2023-03-01
PHP规范PSR0和PSR4的理解
2023-03-01