package zuoye;
import java.util.*;
public class lianxi_3 {
public static void main(String[] args) {
// test2();
// test3();
// test4();
// test5();
test6();
}
// 完成下面的要求
// 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();
list.add(new Worker("zhang3", 18, 3000));
list.add(new Worker("li4", 25, 3500));
list.add(new Worker("wang5", 22, 3200));
// System.out.println(list);
list.add(1, new Worker("zhao6", 24, 3300));
list.remove(3);
System.out.println(list);
for (Object i : list) {
System.out.println(i);
}
Iterator<Worker> it = list.iterator();
while (it.hasNext()) {
Worker netx = it.next();
netx.work();
System.out.println(netx);
}
}
//2.去除集合中字符串的重复值(要求使用 ArrayList)
// 执行结果如下:
// 旧集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 李玉伟, 王健林, 马云, 雷军]
// 新集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 王健林, 马云, 雷军]
public static void test2() {
List<String> list = new ArrayList();
list.add("李玉伟");
list.add("李嘉诚");
list.add("马化腾");
list.add("刘强东");
list.add("李玉伟");
list.add("王健林");
list.add("马云");
list.add("雷军");
System.out.println(list);
List<String> temp = new ArrayList<String>();
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String str = iterator.next();
if (!temp.contains(str)) {
temp.add(str);
}
}
for (String i : temp) {
System.out.println(i);
}
}
//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() {
int[] intRandom=new int[10];
List list=new ArrayList();
Random rd=new Random();
while (list.size()<10){
int num=rd.nextInt(100);
if (!list.contains(num)){
list.add(num);
}
}
System.out.println(list);
}
// 4.编写一个类Book,具有name,price,press(出版社),
// author 然后创建5个对象放入ArrayList中,并实现按照price大小排序,
// 然后遍历ArrayList输出每个Book对象, 使用toString 方法打印。
public static void test4() {
List list = new ArrayList();
list.add(new Book("盗墓笔记", 35, "清华出版社", "南派三叔"));
list.add(new Book("诛仙", 48, "华夏出版社", "萧梁"));
list.add(new Book("在精神病院斩神", 33, "中华出版社", "三九音域"));
list.add(new Book("西游记", 55, "清华出版社", "吴承恩"));
for (Object i : list) {
System.out.println(i);
}
Collections.sort(list);
for (Object i : list) {
System.out.println(i);
}
}
//5.使用List集合存储10个学生信息。
// 学生信息:姓名,年龄,成绩。
// 统计所有姓“张”的同学的平均成绩。
public static void test5() {
List list = new ArrayList();
list.add(new Students("张三", 18, 96));
list.add(new Students("李四", 22, 95));
list.add(new Students("王五", 28, 93));
list.add(new Students("张二牛", 18, 86));
list.add(new Students("张铁柱", 38, 66));
list.add(new Students("张山娃", 38, 76));
list.add(new Students("张狗蛋", 14, 93));
list.add(new Students("张二妞", 12, 94));
list.add(new Students("李兰德", 16, 97));
list.add(new Students("王菲菲", 16, 97));
Iterator<Students> iterator = list.iterator();
double sum = 0;
int num = 0;
while (iterator.hasNext()) {
Students next = iterator.next();
if (next.getName().startsWith("张")) {
num++;
sum += next.getFraction();
}
}
double avg = sum / num;
System.out.println(avg);
// System.out.println(Arrays.toString(ans2));
}
// 6.产生10个1-100的随机数,并放到一个数组中,
// 把数组中大于等于10的数字放到一个list集合中,并打印到控制台
public static void test6() {
int q = 1;
int w = 100;
int[] r = new int[10];
for (int i = 0; i < 10; i++) {
int e = ((int) (Math.random() * w - q)) + q;
r[i] = e;
}
List<Integer> list = new ArrayList<Integer>();
for (int i : r) {
if (i>=10){
list.add(i);
}
}
System.out.println(list);
}
}
class Students {
private String name;
private int age;
@Override
public String toString() {
return "Students{" +
"name='" + name + '\'' +
", age=" + age +
", fraction=" + fraction +
'}';
}
private int fraction;
public Students(String name, int age, int fraction) {
this.name = name;
this.age = age;
this.fraction = fraction;
}
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 int getFraction() {
return fraction;
}
public void setFraction(int fraction) {
this.fraction = fraction;
}
}
class Book implements Comparable<Book>{
private String name;
private int price;
private String press;
private String author;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", press='" + press + '\'' +
", author='" + author + '\'' +
'}';
}
public Book(String name, int price, String press, String author) {
this.name = name;
this.price = price;
this.press = press;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public int compareTo(Book o) {
Book b=(Book) o;
return this.price-o.price;
}
}//第四题需要配置的参数等。。。
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; }
@Override
public String toString() {
return "Worker{" +
"age=" + age +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
public void work(){
System.out.println(name + "work" );
}
}//第一题需要配置的参数等