1.已知有一个Worker 类如下:
public class Worker {
private int age;
private String name;
private double salary;
public Worker (){
}
public Worker (String name, int age, double salary){
this.name = name;
this.age = age;
this.salary = salary;
}
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getSalary(){ return salary; }
public void setSalary(double salary){ this.salary = salary; }
public void work(){
System.out.println(name + “ work”);
}
}
完成下面的要求
1) 创建一个List,在List 中增加三个工人,基本信息如下:
姓名 年龄 工资
zhang3 18 3000
li4 25 3500
wang5 22 3200
2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
3) 删除wang5 的信息
4) 利用for 循环遍历,打印List 中所有工人的信息
5) 利用迭代遍历,对List 中所有的工人调用work 方法。
public static void test1() {
//可以实现此题,但不合适
List<Worker> list = new ArrayList<>();
Worker z = new Worker("zhang3", 18, 3000);
list.add(z);
Worker l = new Worker("li4", 25, 3500);
list.add(l);
Worker w = new Worker("wang5", 22, 3200);
list.add(w);
Worker zh = new Worker("zhao6", 24, 3300);
list.add(list.indexOf(l), zh);
list.remove(list.indexOf(w));
for (Worker i : list) {
System.out.println(i);
}
Iterator<Worker> iterator = list.iterator();
while (iterator.hasNext()) {
Worker i = iterator.next();
System.out.println(i);
i.work();
}
}
2.去除集合中字符串的重复值(要求使用 ArrayList)
执行结果如下:
旧集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 李玉伟, 王健林, 马云, 雷军]
新集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 王健林, 马云, 雷军]
public static void test2() {
ArrayList<String> arrList = new ArrayList<>();
arrList.add("李玉伟");
arrList.add("李嘉诚");
arrList.add("马化腾");
arrList.add("刘强东");
arrList.add("李玉伟");
arrList.add("王健林");
arrList.add("马云");
arrList.add("雷军");
for (int i = 0; i < arrList.size(); i++) {
for (int j = 0; j < i; j++) {
if (arrList.get(i).equals(arrList.get(j))) {
arrList.remove(i);
i--;
}
}
}
System.out.println(arrList.toString());
}
3.分析以下需求,并用代码实现:(使用ArrayList)
(1)生成10个1至100之间的随机整数(不能重复),存入一个List集合
(2)编写方法对List集合进行排序
(2)然后利用迭代器遍历集合元素并输出
(3)如:15 18 20 40 46 60 65 70 75 91
public static void test3(){
Random r = new Random();
ArrayList<Integer> arrayList = new ArrayList();
int num ;
while (arrayList.size()<10){
num = r.nextInt(1,101);
if (!arrayList.contains(num)){
arrayList.add(num);
}
}
MyComparator myComparator = new MyComparator();
arrayList.sort(myComparator);
Iterator<Integer> iterator =arrayList.iterator();
while (iterator.hasNext()){
num = iterator.next();
System.out.print(num+"\t");
}
}
class MyComparator implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
}
4.编写一个类Book,具有name,price,press(出版社),author
class Book{
String name;
int price;
String press;
String author;
public Book(String name, int price, String press, String author) {
this.name = name;
this.price = price;
this.press = press;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", press='" + press + '\'' +
", author='" + author + '\'' +
'}';
}
}
然后创建5个对象放入ArrayList中,并实现按照price大小排序,然后遍历ArrayList输出每个Book对象, 使用toString 方法打印。
public static void test4(){
List<Book> books = new ArrayList<>();
books.add(new Book("西游记",200,"张三出版社","吴承恩"));
books.add(new Book("水浒传",180,"李四出版社","施耐庵"));
books.add(new Book("红楼梦",190,"王五出版社","曹雪芹"));
books.add(new Book("三国演义",210,"赵六出版社","罗贯中"));
books.add(new Book("史记",240,"林七出版社","司马迁"));
Book temp;
for (int i = 0; i < books.size(); i++) {
for (int j = 0; j < i; j++) {
if (books.get(i).price<books.get(j).price){
temp =books.get(j);
books.set(j,books.get(i));
books.set(i,temp);
}
}
}
System.out.println(books);
}
5.使用List集合存储10个学生信息。
学生信息:姓名,年龄,成绩。
class Student {
private String name;
private int age;
private double score;
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
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;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
统计所有姓“张”的同学的平均成绩
public static void test5() {
List<Student> students = new ArrayList<>();
Student student1 = new Student("张三", 18, 91.5);
Student student2 = new Student("张四", 18, 92.5);
Student student3 = new Student("张五", 18, 93.5);
Student student4 = new Student("张六", 18, 80);
Student student5 = new Student("李三", 18, 92);
Student student6 = new Student("李三", 18, 78);
Student student7 = new Student("康三", 18, 62);
Student student8 = new Student("康四", 18, 68);
Student student9 = new Student("康五", 18, 94);
Student student10 = new Student("康奎", 18, 81);
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
students.add(student5);
students.add(student6);
students.add(student7);
students.add(student8);
students.add(student9);
students.add(student10);
double sumScore = 0;
int index = 0;
for (Student i : students) {
if (i.getName().startsWith("张")) {
index++;
sumScore += i.getScore();
}
}
double ayg = sumScore / index;
System.out.println(ayg);
}
6.产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台
public static void test6() {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 100 + 1);
}
List<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
list.add(arr[i]);
}
}
}