day07-常用类-ArrayList-操作练习

This commit is contained in:
2026-01-20 10:34:36 +08:00
parent ed92b5ca56
commit d45f0ae07f
3 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package com.inmind.arraylist03;
import java.util.ArrayList;
/*
常用类_ArrayList_添加对象
创建一个保存学生类对象的集合,对每个学生进行遍历操作
*/
public class Demo10 {
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<>();
//创建3个学生
Student s1 = new Student();
Student s2 = new Student("张三");
studentList.add(s1);
studentList.add(s2);
//使用匿名对象添加到集合中
studentList.add(new Student("李四", 20));
System.out.println(studentList.size());//3
//可以使用for循环对集合的每个元素进行操作
for (int i = 0; i < studentList.size(); i++) {
Student s = studentList.get(i);
System.out.println(s);
System.out.println(s.getName());
System.out.println(s.getAge());
}
}
}

View File

@@ -0,0 +1,8 @@
package com.inmind.arraylist03;
/*
ArrayList_练习1_指定格式拼接字符串
定义以指定格式打印集合的方法(ArrayList类型作为参数),使用@分隔每个元素。
格式参照: [元素@元素@元素]。
*/
public class Test11 {
}

View File

@@ -0,0 +1,10 @@
package com.inmind.arraylist03;
/*
常用类_ArrayList_练习2_获取偶数集合
有一个原本的集合该集合中有正整数1,3,4,5,8,9
将集合中的偶数都取出来,保存,奇数不要
实现方式要有2种1.创建新集合实现 2.不创建新集合实现
*/
public class Test12 {
}