案例一: Collection集合存储学生对象,使用程序实现在控制台遍历集合
思路: 1.定义学生类
2.创建Collection集合对象
3.创建学生对象
4.把学生添加到集合
5.遍历集合(迭代器方式)
学生类
package test.test51;
// 1.定义学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
测试类
package test.test51;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionDemo {
public static void main(String[] args) {
// 2.创建Collection集合对象
Collection<Student> c = new ArrayList<Student>();
// 3.创建学生对象
Student s1 = new Student("张三",30);
Student s2 = new Student("李四",35);
Student s3 = new Student("王五",33);
// 4.把学生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
// 5.遍历集合(迭代器方式)
Iterator<Student> it = c.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+","+s.getAge());
}
}
}
案例二: List集合存储学生对象并遍历
需求: 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台比那里该集合
思路: 1.定义学生类
2.创建List集合对象
3.创建学生对象
4.创建学生添加到集合
5.遍历集合(迭代器方式,for循环方式)
学生类
package test.test52;
// 1.定义学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
测试类
package test.test52;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
// 2.创建List集合对象
List<Student> list = new ArrayList<Student>();
// 3.创建学生对象
Student s1 = new Student("张三",30);
Student s2 = new Student("李四",35);
Student s3 = new Student("王五",33);
// 4.创建学生添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
// 5.遍历集合(迭代器方式,for循环方式)
// 迭代器方式
Iterator<Student> it = list.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
// for循环方式
for (int i = 0; i< list.size(); i++){
Student s = list.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
}
案例三: List集合存储学生对象用三种方式遍历
需求: 创建一个储存学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
思路: 1.定义学生类
2.创建List集合对象
3.创建学生对象
4.把学生对象添加到集合
5.遍历集合
迭代器: 集合特有的方式
普通for: 带有索引的遍历方式
增强for: 最方便的遍历方式
学生类
package test.test53;
// 1.定义学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
测试类
package test.test53;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
// 2.创建List集合对象
List<Student> list = new ArrayList<Student>();
// 3.创建学生对象
Student s1 = new Student("张三",30);
Student s2 = new Student("李四",35);
Student s3 = new Student("王五",33);
// 4.把学生对象添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
// 5.遍历集合
// 迭代器: 集合特有的方式
Iterator<Student> it = list.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
// 普通for: 带有索引的遍历方式
for (int i = 0; i<list.size(); i++){
Student s = list.get(i);
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
// 增强for: 最方便的遍历方式
for (Student s : list){
System.out.println(s.getName()+","+s.getAge());
}
}
}
案例四: ArrayList集合学生对象用三种方式遍历
需求: 创建一个存储学生对象,存储3个学生对象,使用程序实现在控制台遍历该集合
思路: 1.定义学生类
2.创建ArrayList集合对象
3.创建学生对象
4.把学生对象添加到集合
5.遍历集合
迭代器: 集合特有的方式
普通for: 带有索引的遍历方式
增强for: 最方便的遍历方式
学生类
package test.test54;
// 1.定义学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
测试类
package test.test54;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo {
public static void main(String[] args) {
// 2.创建ArrayList集合对象
ArrayList<Student> list = new ArrayList<Student>();
// 3.创建学生对象
Student s1 = new Student("张三",30);
Student s2 = new Student("李四",35);
Student s3 = new Student("王五",33);
// 4.把学生对象添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
// 5.遍历集合
// 迭代器: 集合特有的方式
Iterator<Student> it = list.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
// 普通for: 带有索引的遍历方式
for (int i = 0; i<list.size(); i++){
Student s = list.get(i);
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
// 增强for: 最方便的遍历方式
for (Student s : list){
System.out.println(s.getName()+","+s.getAge());
}
}
}
案例五: HashSet集合存储学生对象并遍历
需求: 创建一个存储学生对象的集合,存储多个学生对象,使用程序实现在控制台遍历该集合
要求: 学生对象的成员变量值相同,我们就认为是同一个对象
思路: 1.定义学生类
2.创建HashSet集合对象
3.创建学生对象
4.把学生添加到集合
5.遍历集合(增强for)
6.在学生类中重写两个方法
hashCode()和equals()
自动生成
学生类
package test.test55;
// 1.定义学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
// 6.在学生类中重写两个方法
// equals
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
// hashCode
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
测试类
package test.test55;
import java.util.HashSet;
// 要求: 学生对象的成员变量值相同,我们就认为是同一个对象
public class HashSetDemo {
public static void main(String[] args) {
// 2.创建HashSet集合对象
HashSet<Student> hs = new HashSet<Student>();
// 3.创建学生对象
Student s1 = new Student("张三",30);
Student s3 = new Student("李四",35);
Student s2 = new Student("王五",33);
Student s4 = new Student("李四",35);
// 4.把学生添加到集合
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
// 5.遍历集合(增强for)
for (Student s : hs){
System.out.println(s.getName()+","+s.getAge());
}
}
}
案例六: 成绩排序
需求: 用TreeSet集合存储多个学生信息(姓名,语文成绩,数学成绩),并遍历该集合
要求: 按照总分从高到低出现
思路: 1.定义学生类
2.创建TreeSet集合对象,通过比较器排序进行排序
3.创建学生对象
4.把学生对象添加到集合
5.遍历集合
学生类
package test.test56;
// 1.定义学生类
public class Student {
private String name;
private int chinese;
private int math;
public Student() {
}
public Student(String name, int chinese, int math) {
this.name = name;
this.chinese = chinese;
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getSum(){
return this.chinese+this.math;
}
}
测试类
package test.test56;
import java.util.Comparator;
import java.util.TreeSet;
// 要求: 按照总分从高到低出现
public class TreeSetDemo {
public static void main(String[] args) {
// 2.创建TreeSet集合对象,通过比较器排序进行排序
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
int num2 = num == 0 ? s1.getChinese()-s2.getChinese() : num;
int num3 = num2 == 0 ? s1.getName().compareTo(s2.getName()) : num2;
return num3;
}
});
// 3.创建学生对象
Student s1 = new Student("张三",98,100);
Student s2 = new Student("李四",95,95);
Student s3 = new Student("王五",100,93);
Student s4 = new Student("赵六",100,97);
Student s5 = new Student("孙七",98,98);
Student s6 = new Student("周八",97,99);
Student s7 = new Student("吴九",97,99);
// 4.把学生对象添加到集合
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
ts.add(s7);
// 5.遍历集合
for (Student s : ts){
System.out.println(s.getName()+",语文成绩:"+s.getChinese()+",数学成绩:"+s.getMath()+",总分为"+s.getSum());
}
}
}
案例七: 不重复的随机数
需求: 编写一个程序,获取10个1~20之间的随机数,要求不能重复,并在控制台输出
思路: 1.创建Set集合对象
2.创建随机数对象
3.判断集合的长度是不是小于10
是: 添加一个随机数
回到3继续
4.遍历集合
HashSet
package test;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Test57 {
public static void main(String[] args) {
// 1.创建Set集合对象
Set<Integer> s = new HashSet<Integer>();
// 2.创建随机数对象
Random r = new Random();
// 3.判断集合的长度是不是小于10
while (s.size() < 10){
//是: 添加一个随机数
//回到3继续
int number = r.nextInt(20)+1;
s.add(number);
}
// 4.遍历集合
for (int i : s){
System.out.println(i);
}
}
}
TreeSet
package test;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class Test57 {
public static void main(String[] args) {
// 1.创建Set集合对象
Set<Integer> s = new TreeSet<Integer>();
// 2.创建随机数对象
Random r = new Random();
// 3.判断集合的长度是不是小于10
while (s.size() < 10){
//是: 添加一个随机数
//回到3继续
int number = r.nextInt(20)+1;
s.add(number);
}
// 4.遍历集合
for (int i : s){
System.out.println(i);
}
}
}