题目1
某班级有若干学生,学生对象存放在一个 List 中,每个学生有姓名(String)、班级(String)、成绩(double)属性,某次考试成绩结束后每个学生都获得了成绩。遍历list集合,把所有学生的属性都打印出来。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Student {
private String name;
private String classes;
private double score;
public Student(String name, String classes, double score) {
this.name = name;
this.classes = classes;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", classes='" + classes + '\'' +
", score=" + score +
'}';
}
}
public class TestDemo {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("bit", "102-1", 98.9));
students.add(new Student("张三", "102-1", 88.8));
students.add(new Student("李四", "102-1", 90.0));
System.out.println(students);
}
}
题目2
删除第一个字符串当中出现的第二个字符串中的字符: 如:
思路:获取第一个字符串str1的每个字符,创建一个ArrayList对象 list ,将第二个字符串str2不包含str1中出现的字符add()到 list 中
String str1 = "welcome to cqupt";
String str2 = "come";
输出:wl t qupt
public class TestDemo {
public static void main(String[] args) {
String str1 = "welcome to cqupt";
String str2 = "come";
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < str1.length(); i++) {
char ch = str1.charAt(i);
if (!str2.contains(ch + "")) {
list.add(ch);
}
}
for (char ch : list) {
System.out.print(ch);
}
}
}
题目3
有一个List 中存放的整形的数据,要求使用Collections.sort 对List 进行排序。
public static void main(String[] args) {
ArrayList<Integer> integers = new ArrayList<>();
integers.add(11);
integers.add(2);
integers.add(13);
Collections.sort(integers);
System.out.println(integers);
}
|