一、 基础案例
1. 训练案例1
1.1. 训练描述:【Collection接口】
一、 需求说明:自定义一个学生类,给出成员变量name和age,使用Collection集合存储自定义对象并遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。
1.2. 操作步骤描述
- 创建学生类。
- 创建集合对象。
- 创建元素对象。
- 把元素添加到集合。
- 遍历集合。
public abstract class Test01 {
public static void main(String[] args) {
Collection<Student> col = new ArrayList<Student>();
Student a = new Student("小明",24);
Student b= new Student("大华",26);
Student c = new Student("婷婷",18);
col.add(a);
col.add(b);
col.add(c);
Iterator<Student> it = col.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println("姓名:"+s.getName()+",年龄"+s.getAge());
}
}
}
2. 训练案例2
2.1. 训练描述:【List接口、迭代器、普通for】
一、 需求说明:自定义一个学生类,给出成员变量name和age,使用List集合存储自定义对象并遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。要求使用两种方式进行遍历(迭代器、普通for)。
2.2. 操作步骤描述
- 创建学生类。
- 创建集合对象。
- 创建元素对象。
- 把元素添加到集合。
- 遍历集合。
public class Student {
String name ;
int age ;
public Student() {
}
public Student(String name, int age) {
super();
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;
}
}
import java.util.List;
//import java.awt.List; List<Student>
//list = new ArrayList<Student>();会报错,awt 改成util即可
import java.util.ArrayList;
import java.util.Iterator;
public class Test02 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student a = new Student("小明", 24);
Student b = new Student("大华", 26);
Student c = new Student("婷婷", 18);
list.add(a);
list.add(b);
list.add(c);
Iterator<Student> it = list.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println("姓名:"+s.getName()+",年龄"+s.getAge());
}
System.out.println("------------------------------------");
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
System.out.println("姓名:"+s.getName()+",年龄"+s.getAge());
}
}
}
3. 训练案例3
3.1. 训练描述:【List接口、增强for】
一、 需求说明:自定义一个学生类,给出成员变量name和age,使用List集合存储自定义对象并使用增强for进行遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。
3.2. 操作步骤描述
- 创建学生类。
- 创建集合对象。
- 创建元素对象。
- 把元素添加到集合。
- 遍历集合。
import day11.test01.Student;
import java.util.ArrayList;
import java.util.List;
public abstract class Test03 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student a = new Student("小明", 24);
Student b = new Student("大华", 26);
Student c = new Student("婷婷", 18);
list.add(a);
list.add(b);
list.add(c);
for(Student s :list){
System.out.println("姓名:"+s.getName()+",年龄"+s.getAge());
}
}
}
4. 训练案例4
4.1. 训练描述:【ArrayList类】
需求说明:自定义一个学生类,给出成员变量name和age,使用List集合存储自定义对象并行遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。要求使用三种方式进行遍历(迭代器、普通for、增强for)。
4.2. 操作步骤描述
- 创建学生类。
- 创建集合对象。
- 创建元素对象。
- 把元素添加到集合。
- 遍历集合。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import day11.test01.Student;
public abstract class Test04 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student a = new Student("小明", 24);
Student b = new Student("大华", 26);
Student c = new Student("婷婷", 18);
list.add(a);
list.add(b);
list.add(c);
Iterator<Student> it = list.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println("姓名:"+s.getName()+",年龄"+s.getAge());
}
}
}
二、 扩展案例
1. 训练案例1
1.1. 训练描述:
一、 分析以下需求,并用代码实现:
- 按照以下描述完成类的定义。
学生类
属性:
姓名name
年龄age
成绩score
行为:
吃饭eat()
stuty(String content)(content:表示学习的内容) - 定义学生工具StudentsTool,有四个方法,描述如下
public void listStudents(Student[] arr):遍历打印学生信息
public int getMaxScore(Student[] arr):获取学生成绩的最高分
public Student getMaxStudent(Student[] arr):获取成绩最高的学员
public int getAverageScore(Student[] arr):获取学生成绩的平均值
public int getCount(Student[] arr):获取不及格的学员数量 - 定义测试类TestStudentTool,在main方法中首先创建长度为5的Student数组并初始化数据,再创建StudentsTool类的对象,并调用以上方法
public class StudentTool {
public void listStudents(Student[] arr) {
for (Student s : arr) {
System.out.println("姓名:" + s.getName() + ",年龄:" + s.getAge() + ",分数:" + s.getScore());
}
}
public int getMaxScore(Student[] arr) {
int max = 0;
for (int i = 0; i < arr.length; i++) {
Student s = arr[i];
max = max >= s.getScore() ? max : s.getScore();
}
return max;
}
public int getAverageScore(Student[] arr) {
int avg = 0;
for (int i = 0; i < arr.length; i++) {
Student s = arr[i];
avg += s.getScore();
}
avg = avg / arr.length;
return avg;
}
public Student getMaxStudent(int max, Student[] arr) {
Student s =null;
for (int i = 0; i < arr.length; i++) {
if (max == arr[i].getScore()) {
s = arr[i];
}
}
return s;
}
public int getCount(Student[] arr){
int sum = 0;
for (int i = 0; i < arr.length; i++) {
Student s = arr[i];
if(s.getScore()<60){
sum++;
}
}
return sum;
}
}
import java.util.ArrayList;
import java.util.List;
public class Main01 {
public static void main(String[] args) {
Student a = new Student("陈一",21,22);
Student b = new Student("牛二",22,58);
Student c = new Student("张三",23,85);
Student d = new Student("李四",24,88);
Student e = new Student("王五",25,92);
Student[] arr = {a,b,c,d,e};
StudentTool tool = new StudentTool();
tool.listStudents(arr);
int max = tool.getMaxScore(arr);
System.out.println("最高分:"+max);
int avg = tool.getAverageScore(arr);
System.out.println("平均分:"+avg);
Student s = tool.getMaxStudent(max, arr );
System.out.println("姓名:" + s.getName() + ",年龄:" + s.getAge() + ",分数:" + s.getScore());
int sum = tool.getCount(arr);
System.out.println("不及格人数:"+sum);
}
}
2. 训练案例2
2.1. 训练描述:
一、 分析以下需求,并用代码实现
- 定义ArrayList集合,存入多个字符串"abc" “def” “efg” “def” “def” “qwe” “def” “def” “swd” “wwe” “def” “def”
- 使用普通for循环获取集合中索引为3的元素并打印
- 定义方法public static boolean myContains(ArrayList list,String str)
(1)参数
a.ArrayList list: 表示存储多个String数据的集合
b.String str: 表示一个字符串
(2)返回值
true: 表示list集合中包含字符串str
false: 表示list集合中不包含字符串str - 利用上面定义的mycontains方法统计集合中包含字符串"def"的数量
- 删除集合中的所有字符串"def"(思路:循环判断集合中是否包含"def"字符串,包含就删除)
- 将集合中每个元素中的小写字母变成大写字母
import java.util.ArrayList;
import java.util.Collection;
public class Main02 {
public static void main(String[] args) {
String[] str = {"abc", "def" , "efg", "def" ,"def" ,"qwe" ,"def" ,"def", "swd" ,"wwe", "def" ,"def"};
ArrayList<String> list = new ArrayList();
for (int i = 0; i < str.length; i++) {
list.add(str[i]);
}
System.out.println(list.get(3));
System.out.println();
String s = "def";
boolean b = myContains(list , s);
System.out.println("list集合中是否包含字符串:"+s);
System.out.println(b);
System.out.println();
int sum = 0;
while(b){
sum++;
list.remove(s);
b = myContains(list , s);
}
System.out.println("list集合中是否包含字符串:"+s +"次数:"+sum);
}
public static boolean myContains(ArrayList list,String str){
return list.contains(str);
}
}
3. 训练案例3
3.1. 训练描述:
一、 分析以下需求,并用代码实现
- 定义Student类
属性:
姓名:String name
年龄:int age
成绩:int score
行为:
空参构造方法
有参构造方法
set和get方法
toString方法 - 定义测试类,进行测试
(1)创建10个学生对象存入ArrayList集合中
(2)打印最高分的学员姓名、年龄、成绩 [要求封装1个方法 参数是集合对象 返回值类型为Student]
(3)打印10个学生的总成绩和平均分 [要求封装两个方法完成]
(4)打印不及格的学员信息及数量 [要求封装一个方法完成]
public class Main03 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("陈一", 21, 22));
list.add(new Student("牛二", 22, 58));
list.add(new Student("张三", 23, 85));
list.add(new Student("李四", 23, 82));
list.add(new Student("王五", 25, 92));
list.add(new Student("陈发六", 23, 64));
list.add(new Student("牛是七", 22, 56));
list.add(new Student("张的八", 24, 45));
list.add(new Student("李发九", 25, 88));
list.add(new Student("王啊十", 27, 95));
Student s = max(list);
System.out.println("最高分的学员姓名、年龄、成绩:");
System.out.println(s.toString());
System.out.println();
int sum = sum(list);
System.out.println("10个学生的总成绩:"+sum);
System.out.println();
int avg = avg(list);
System.out.println("10个学生的平均分为:"+avg);
System.out.println();
gua(list);
}
public static Student max(ArrayList<Student> list) {
Student s = list.get(0);
for (int i = 0; i < list.size(); i++) {
s = s.getScore() >= list.get(i).getScore() ? s : list.get(i);
}
return s;
}
public static int sum(ArrayList<Student> list){
int sum = 0;
for (int i = 0; i < list.size(); i++) {
sum += list.get(i).getScore();
}
return sum;
}
public static int avg(ArrayList<Student> list){
int avg =0;
int sum = 0;
for (int i = 0; i < list.size(); i++) {
sum += list.get(i).getScore();
}
return avg = sum/list.size();
}
public static void gua(ArrayList<Student> list){
int count = 0;
System.out.println("不及格学生有:");
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getScore()<60){
System.out.println(list.get(i).toString());
count++;
}
}
System.out.println("不及格人数一共人数:"+count);
}
}
4. 训练案例4
4.1. 训练描述:
一、 分析以下需求,并用代码实现
- 定义ArrayList集合,存入多个字符串
如:“ab1” “123ad” “bca” “dadfadf” “dddaaa” “你好啊” “我来啦” “别跑啊” - 遍历集合,删除长度大于5的字符串,打印删除后的集合对象
提示:可以将原集合中所有长度大于5的字符串放入到新集合中,遍历新集合,新集合中的元素就是要删除的元素 - 基于上一步,删除集合中元素包含0-9数字的字符串(只要字符串中包含0-9中的任意一个数字就需要删除此整个字符串)
提示:
(1)定义public static boolean myContains(String str)方法
功能:
判断str中是否包含0到9的数字
包含:返回true
不包含:返回false
(2)遍历原集合利用myContains方法将所有包含0-9的字符串放入新集合中
(3)新集合中的元素就是要删除的元素
public class Main04 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("ab1");
list.add("123ad");
list.add("bca");
list.add("dadfadf");
list.add("dddaaa");
list.add("你好啊");
list.add("我来啦" );
list.add("别跑啊");
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if(s.length()>=5){
list.remove(i);
i--;
}
}
for(String s : list){
System.out.println(s);
}
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < 10; j++) {
String k = String.valueOf(j);
if(list.get(i).contains(k)){
System.out.println(list.get(i)+"将要删除,它包含0~9数字:"+k);
list.remove(i);
}
}
}
System.out.println("剩下的字符:");
for(String s : list){
System.out.println(s);
}
}
}
5. 训练案例5
5.1. 训练描述:
一、 分析以下需求,并用代码实现
定义MyArrays工具类,该工具类中有以下方法,方法描述如下:
- public static void reverse(ArrayList list);
参数ArrayList list:要进行操作的集合对象
要求:对list集合对象中的元素进行反转(第一个和最后一个交换,第二个和倒数第二个交换,第三个和倒数第三个交换…) - public static Integer max(ArrayList list);
参数ArrayList list:要进行操作的集合对象
要求:求出list集合对象中的最大值并返回
public class MyArrys {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
reverse(list);
Integer max = max(list);
System.out.println("最大值:"+max);
}
public static void reverse(ArrayList<Integer> list){
Integer a = 0;
for (int i = 0; i < list.size(); i++) {
a = list.get(i) ;
list.set(list.get(i),list.get(list.size()-1-i));
list.set(list.get(list.size()-1-i),a);
System.out.println(list.get(i));
}
}
public static Integer max(ArrayList<Integer> list){
Integer max = 0;
for (int i = 0; i < list.size(); i++) {
max = max >= list.get(i) ? max :list.get(i);
}
return max;
}
}