1-4练习

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 class Task1 {

public static void main(String[] args) {

List <Worker>list=new LinkedList<>();

Worker zhang3=new Worker("zhang3",18,3000);

Worker li4=new Worker("li4",25,3500);

Worker wang5=new Worker("wang5",22,3200);

// 创建一个List,在List 中增加三个工人

list.add(zhang3);

list.add(li4) ;

list.add(wang5);

// 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300

Worker zhao6=new Worker("zhao6",24,3300);

list.add(list.indexOf(li4),zhao6);

// 删除wang5 的信息

list.remove(list.indexOf(wang5));

// 利用for 循环遍历,打印List 中所有工人的信息

for (Worker i: list

) {

System.out.println(i.toString());

}

// 利用迭代遍历,对List 中所有的工人调用work 方法。

Iterator <Worker>iterator=list.iterator();

while(iterator.hasNext()){

Worker i=iterator.next() ;

i.work();

}

}

}

2.去除集合中字符串的重复值(要求使用 ArrayList)

执行结果如下:

旧集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 李玉伟, 王健林, 马云, 雷军]

新集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 王健林, 马云, 雷军]

public class Task2 {

public static void main(String[] args) {

ArrayList<String> arrayList = new ArrayList();

arrayList.add("李玉伟");

arrayList.add("李嘉诚");

arrayList.add("马化腾");

arrayList.add("刘强东");

arrayList.add("李玉伟");

arrayList.add("王健林");

arrayList.add("马云");

arrayList.add("雷军");

Iterator<String> iterator1 = arrayList.iterator();

Iterator<String> iterator2 = arrayList.iterator();

for (int i = 0; i < arrayList.size(); i++) {

for (int j = 0; j < i; j++) {

if (arrayList.get(i).equals(arrayList.get(j))) {

arrayList.remove(i);

i--;

}

}

}

System.out.println(arrayList);

}

}

3.分析以下需求,并用代码实现:(使用ArrayList)

(1)生成10个1至100之间的随机整数(不能重复),存入一个List集合

(2)编写方法对List集合进行排序

(2)然后利用迭代器遍历集合元素并输出

(3)如:15 18 20 40 46 60 65 70 75 91

public class Task3 {

public static void main(String[] args) {

Random r=new Random();

ArrayList<Integer> arrayList=new ArrayList();

int num=0;

while(num<10){

int i;

int x=r.nextInt(1,101);

for( i=0;i<arrayList.size();i++){

if(x==arrayList.get(i))

break;

}

if (i==arrayList.size())

{

arrayList.add(x);

num++;

}

}

MyComparator myComparator=new MyComparator();

arrayList.sort(myComparator);

Iterator<Integer> iterator=arrayList.iterator();

while(iterator.hasNext()){

int x=iterator.next();

System.out.print(x+"\t");

}

}

}

class MyComparator implements Comparator<Integer>{

@Override

public int compare(Integer o1, Integer o2) {

return o1 - o2;

}

}

4.编写一个类Book,具有name,price,press(出版社),author 然后创建5个对象放入ArrayList中,并实现按照price大小排序,

然后遍历ArrayList输出每个Book对象, 使用toString 方法打印。

public class Task4 {

public static void main(String[] args) {

Book book1 =new Book("book1",15.5f,"public1","author1");

Book book2 =new Book("book2",13.5f,"public2","author2");

Book book3 =new Book("book3",16.5f,"public3","author3");

Book book4 =new Book("book4",20,"public4","author4");

Book book5 =new Book("book5",15,"public5","author5");

ArrayList<Book> bookArrayList=new ArrayList<>();

bookArrayList.add(book1);

bookArrayList.add(book2);

bookArrayList.add(book3);

bookArrayList.add(book4);

bookArrayList.add(book5);

BookComparator bookComparator=new BookComparator();

bookArrayList.sort(bookComparator);

for (Book i: bookArrayList

) {

System.out.println(i.toString());

}

}

}

class BookComparator implements Comparator<Book> {

public int compare(Book o1, Book o2) {

if(o1.price >o2.price) return 1;

else return -1;

}

}

class Book{

String name;

float price;

String press;

String author;

public Book(String name, float 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.使用List集合存储10个学生信息。

学生信息:姓名,年龄,成绩。

统计所有姓“张”的同学的平均成绩。

public class Task5 {

public static void main(String[] args) {

Student stu1=new Student("张1",20,88);

Student stu2=new Student("张2",21,89);

Student stu3=new Student("张3",22,90);

Student stu4=new Student("李4",20,88);

Student stu5=new Student("王5",20,88);

Student stu6=new Student("马6",20,88);

Student stu7=new Student("宋7",20,88);

Student stu8=new Student("艾8",20,88);

Student stu9=new Student("赵9",20,88);

Student stu10=new Student("金10",20,88);

ArrayList<Student> student=new ArrayList<>();

student.add(stu1);

student.add(stu2);

student.add(stu3);

student.add(stu4);

student.add(stu5);

student.add(stu6);

student.add(stu7);

student.add(stu8);

student.add(stu9);

student.add(stu10);

int num=0;

float total=0;

for (Student i:student) {

if(i.name.charAt(0)=='张')

{

num++;

total+=i.score;

}

}

float avg;

if(num==0)

avg=0;

else

avg=total/num;

System.out.println("姓张同学的平均成绩为"+avg);

}

}

class Student{

String name;

int age;

float score;

public Student(String name, int age, float score) {

this.name = name;

this.age = age;

this.score = score;

}

}

6.产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台

public class Task6 {

public static void main(String[] args) {

int [] arrInt=new int[10];

Random r=new Random();

for(int i=0;i<10;i++)

{

arrInt[i]=r.nextInt(1,101);

}

ArrayList list=new ArrayList();

for(int i=0;i<10;i++){

list.add(arrInt[i]);

}

System.out.println(list);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值