List集合
习题:
1:完成以下填空:
List 接口的特点是元素 有序 (有|无)顺序, 可重复 (可以|不可以)重复;
2:(List)关于List 集合描述正确的是()
A.可以利用List 直接创建集合对象,用于存储多个数据
B.List 集合可以利用下标操作集合中的元素
C.Collection 是List 的父接口(正确)
D.List 中的功能方法全部继承于父接口
3:(List)欲构造ArrayList 类的一个实例,此类继承了 List 接口,下列哪个方法是正确的()
ArrayList myList = new Object(); 顶级父类
List myList = new ArrayList();
ArrayList myList = new List();
List myList = new List();
4:(List)仔细阅读以下代码,写出程序运行的结果。
Hello
Learn
5:(List)简单介绍ArrayList、LinkedList、Vector 的特性及区别。
ArrayList底层是数组结构 所以他是有序的 查询快,增删慢 线程不安全,效率高
LinkedList底层是链表结构 查询慢 增删快 线程不安全,效率高
Vector 底层数组实现 都慢 线程安全,效率低
6:(List)简述List<String> list1= new ArrayList<String>();和List list2 =new ArrayList();区别及
<String>的作用。
List<String> 有泛型约束,是他只能存放String类型的元素,更加的安全
list没有泛型约束,可以存放不同类型的元素,放的更多,越不安全,有时需要进行类型强转
使类型安全,代码清晰,具有向后兼容性,不用进行类型强转,更加的方便
7:(List)编程:创建一个工人类,属性:姓名、年龄、工资,要求如下:
①为Worker 提供无、有参数的构造方法,属性私有,并提供公开的 get/set
②创建多个Worker 类,存储在List 集合中。
③打印输出所有的工人信息。
④计算所有工人的平均工资。
⑤打印输出姓名中包含 "胡" 的所有工人信息。
⑥打印输出所有姓 "胡" 的工人信息。
⑦键盘输入一个姓名,查找是否存在此员工,存在,则打印输出员工的信息, 如果不存在,则输出"查 无此人"
⑧输入一个工资,查询工资 大于 输入工资的员工信息。
package com.by.homework3;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Worker {private String name;private int age;private double salary;public Worker() {}public Worker(String name, int age, double salary) {this.name = name;this.age = age;this.salary = salary;}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 getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public String toString(){return "姓名:"+name+",年龄:"+age+",薪资:"+salary;}
}class WorkTest{public static void main(String[] args) {List<Worker> list = new ArrayList<>();list.add(new Worker("张三", 18, 2000));list.add(new Worker("李四", 19, 3000));list.add(new Worker("王五", 20, 4000));list.add(new Worker("赵胡麻", 21, 5000));list.add(new Worker("胡八一", 22, 6000));list.forEach(o -> System.out.println(o + " "));System.out.println();System.out.println();System.out.println("---------------------------");double average = 0;for (int i = 0; i<list.size(); i++){average+=list.get(i).getSalary();String s=String.valueOf(list.get(i));if (s.contains("胡")){System.out.println(list.get(i));}}System.out.println();System.out.println("平均工资为:"+average/5);String string=String.valueOf(list);char []chars=string.toCharArray();for (int i = 0; i < chars.length; i++) {if (String.valueOf(chars[i]).equals("胡")){System.out.println(chars[i]);}}Scanner sc=new Scanner(System.in);System.out.println("请输入名字");String s1=sc.next();boolean boo=false;for (int i=0;i<list.size();i++){if(s1.equals(list.get(i).getName())){System.out.println(list.get(i));boo=true;}}if (!boo){System.out.println("不存在");}System.out.println("请输入工资");double d=sc.nextInt();for (int i=0;i<list.size();i++){if(d<list.get(i).getSalary()){System.out.println(list.get(i));boo=true;}}if (!boo){System.out.println("不存在");}}
}
8:(List)编程:已知有 Worker 类,属性为姓名、年龄、工资,完成类的封装并提供无参数、有参数构造方法,完成以下要求:
①创建一个List,在List 中增加三个工人,基本信息如下 :
姓名 年龄 工资
zhang3 18 3000
li4 25 3500
Wang5 22 3200
②在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资 3300
③删除wang5 的信息
④利用下标遍历,打印输出所有工人信息
⑤利用forEach 遍历,打印输出所有年龄大于 20 的工人信息
⑥对Worker 类添加eqauls 方法
package com.by.homework3;import java.util.ArrayList;
import java.util.List;public class Worker1 {private String name;private int age;private double salary;public Worker1() {}public Worker1(String name, int age, double salary) {this.name = name;this.age = age;this.salary = salary;}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 getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public String toString() {return "姓名:" + name + ",年龄:" + age + ",薪资:" + salary;}public boolean equals(Object o) {if (this == o) {return true;}if (this.getClass() != o.getClass() || o.getClass() == null) {return false;}return this.name.equals(((Worker1) o).name);}
}class WorkerTest1 {public static void main(String[] args) {List list = new ArrayList();list.add(new Worker1("zhang3", 18, 3000));list.add(new Worker1("li4", 25, 3500));list.add(new Worker1("wang5", 22, 3200));list.add(1, new Worker1("zhao6", 24, 3300));list.remove(3);for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}System.out.println();System.out.println("--------------------------");for (Object w : list) {Worker1 worker1 = (Worker1) w;if (worker1.getAge() > 20) {System.out.println(w);}}}}
9:(List) 创建一个商品(Produtor)类,属性:商品名,商品单价,商品的数量,商品产地。
① 创建多个商品对象,存储在List集合中。
② 显示所有的商品信息。
③ 打印输出商品价格 > 1000 的所有商品信息。
④ 打印售空的商品的信息。
⑤ 打印输出商品产地为"北京"的商品信息。
⑥ 输入一个商品名,查询出此类商品的信息,如果不存在,则打印商品"商场无此商品!!!"
⑦ 输入一个价格段,查询出集合中所有在这个价格区间的所有商品信息。
package com.by.homework3;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Consumer;public class Produtor {private String name;private double price;private int num;private String production;public Produtor() {}public Produtor(String name, double price, int num, String production) {this.name = name;this.price = price;this.num = num;this.production = production;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getProduction() {return production;}public void setProduction(String production) {this.production = production;}public String toString(){return "商品名:"+name+",价格:"+price+",数量:"+num+",产地:"+production;}
}class ProdutorTest{public static void main(String[] args) {List list=new ArrayList();list.add(new Produtor("小米",4999,99,"北京"));list.add(new Produtor("红米",999,88,"杭州"));list.add(new Produtor("华为",6999,0,"北京"));list.add(new Produtor("苹果",10099,9999,"America"));list.add(new Produtor("杂牌",100,999999,"华强北"));list.forEach(new Consumer() {@Overridepublic void accept(Object o) {System.out.println(o+"");}});System.out.println();System.out.println("------------");for (int i=0;i<list.size();i++){Produtor produtor=(Produtor) list.get(i);if (produtor.getPrice()>1000){System.out.println(produtor);}}System.out.println();for (int i=0;i<list.size();i++){Produtor produtor=(Produtor) list.get(i);if (produtor.getNum()==0){System.out.println("售空产品为:"+produtor);}if(produtor.getProduction().equals("北京")){System.out.println(produtor);}}System.out.println();for (int i=0;i<list.size();i++){Produtor produtor=(Produtor) list.get(i);if(produtor.getProduction().equals("北京")){System.out.println(produtor);}}Scanner scanner=new Scanner(System.in);System.out.println("请输入商品名:");String name=scanner.next();boolean boo=false;for (int i=0;i<list.size();i++){Produtor produtor=(Produtor) list.get(i);if (produtor.getName().equals(name)){System.out.println(produtor);boo=true;}}if (!boo){System.out.println("不存在");}System.out.println("输入低价");double price1=scanner.nextInt();System.out.println("输入高价");double price2=scanner.nextInt();for (int i=0;i<list.size();i++){Produtor produtor=(Produtor) list.get(i);if (produtor.getPrice()>=price1&&produtor.getPrice()<=price2){System.out.println(produtor);}}}
}
10:(List)定义一个用户类(User):---要求封装
① 属性:用户名、密码、是否在线(是-true)
② 提供有参数、无参数的构造方法
③ 创建多个对象,存储在集合中,并统计在线用户数量,打印在控制台上。
package com.by.homework3;import java.util.ArrayList;
import java.util.List;public class User {private String name;private String password;private boolean zaixian;public User() {}public User(String name, String password, boolean zaixian) {this.name = name;this.password = password;this.zaixian = zaixian;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public boolean isZaixian() {return zaixian;}public void setZaixian(boolean zaixian) {this.zaixian = zaixian;}public String toString(){return "用户名"+name+"密码"+password+"是否在线"+zaixian;}
}
class UserTest{public static void main(String[] args) {List list=new ArrayList();list.add(new User("张三","123",true));list.add(new User("李四","312",false));list.add(new User("王五","456",true));for(int i=0;i<list.size();i++){User u=(User)list.get(i);if (u.isZaixian()==true){System.out.println(u);}}}}
11:(List)定义一个员工类(Employee):---要求封装
① 属性:姓名,生日月,工资
② 提供无参数\有参数的构造方法
③ 创建多个员工对象,存储在Employee数组中.
④ 利用toString方法展示所有员工的信息,要求格式为: 张三-10-18000.0
⑤ 控制台输入当前月份,统计过生日的员工数量.
⑥ 控制台输入一个工资,打印工资高于此工资的所有员工信息.
package com.by.homework3;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Employee {private String name;private int birthday;private double salary;public Employee() {}public Employee(String name, int birthday, double salary) {this.name = name;this.birthday = birthday;this.salary = salary;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getBirthday() {return birthday;}public void setBirthday(int birthday) {this.birthday = birthday;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public String toString(){return name+"-"+birthday+"-"+salary;}
}
class EmployeeTest{public static void main(String[] args) {List list=new ArrayList();list.add(new Employee("张三",10,10000));list.add(new Employee("李四",9,20000));list.add(new Employee("王五",8,30000));list.add(new Employee("赵六",7,40000));Scanner sc=new Scanner(System.in);System.out.println("请输入当前月份");int month=sc.nextInt();for (int i=0;i<list.size();i++){Employee e=(Employee) list.get(i);if (e.getBirthday()==month){System.out.println(e);}}System.out.println("请输入工资:");double d=sc.nextInt();for (int i=0;i<list.size();i++){Employee e=(Employee) list.get(i);if (e.getSalary()>d){System.out.println(e);}}}
}
12:(List)在控制台输入格式为“张三/18/男/99.5”的学生若干,存于List集合中。
要求:
①从数组遍历所有内容解析为学生对象,将学生在存于一个新的List集合
②遍历集合找出”优秀”的学生信息(优秀:成绩>=80)
③找出集合中没有参加考试的学生信息(成绩为null)
④排除集合中的重复元素
⑤制定成绩光荣榜(成绩从高到低打印学生成绩)