day07-常用类-ArrayList-操作练习
This commit is contained in:
30
day07/src/com/inmind/arraylist03/Demo10.java
Normal file
30
day07/src/com/inmind/arraylist03/Demo10.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
8
day07/src/com/inmind/arraylist03/Test11.java
Normal file
8
day07/src/com/inmind/arraylist03/Test11.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.arraylist03;
|
||||
/*
|
||||
ArrayList_练习1_指定格式拼接字符串
|
||||
定义以指定格式打印集合的方法(ArrayList类型作为参数),使用@分隔每个元素。
|
||||
格式参照: [元素@元素@元素]。
|
||||
*/
|
||||
public class Test11 {
|
||||
}
|
||||
10
day07/src/com/inmind/arraylist03/Test12.java
Normal file
10
day07/src/com/inmind/arraylist03/Test12.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.inmind.arraylist03;
|
||||
/*
|
||||
常用类_ArrayList_练习2_获取偶数集合
|
||||
有一个原本的集合,该集合中有正整数,1,3,4,5,8,9
|
||||
将集合中的偶数都取出来,保存,奇数不要
|
||||
|
||||
实现方式要有2种:1.创建新集合实现 2.不创建新集合实现
|
||||
*/
|
||||
public class Test12 {
|
||||
}
|
||||
Reference in New Issue
Block a user