小练习 1.4

本文展示了如何使用Java编程语言进行List集合的操作,包括添加、删除、插入元素,以及遍历和排序对象。具体例子涉及到创建Worker对象列表,去重字符串列表,生成随机数列表,按价格排序Book对象,统计特定姓氏学生平均成绩,以及筛选大于10的随机数。
摘要由CSDN通过智能技术生成

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" );

}

}//第一题需要配置的参数等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值