List 练习
(List)已知有一个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 方法。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; public class List2 { public static void main(String[] args)
{
List<Worker> list = new ArrayList<>() ; list.add(new Worker("zhang3" , , ) ) ;
list.add(new Worker("li4" , , ) ) ;
list.add(new Worker("wang5" , , ) ) ; System.out.println("开始时工人状况为:");
for(Worker x : list)
{
System.out.println(x+" ");
} System.out.println("li4之前插入zhao6:"); list.add(, new Worker("zhao6" , , )); for(Worker x : list)
{
System.out.println(x+" ");
} System.out.println("删除wang5:"); list.remove() ; for(Worker x : list)
{
System.out.println(x+" ");
} System.out.println("迭代:"); Iterator<Worker> it = list.iterator() ; while(it.hasNext())
{
Worker w = it.next() ; w.work();
}
} } 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");
} @Override
public String toString() {
return name+" "+age+" "+salary;
} }

随机推荐
- 第八节:pandas字符串
Pandas提供了一组字符串函数,可以方便地对字符串数据进行操作.
- 修改bash命令提示符
说明:PS1是主要的提示符设置,在ubuntu一般为: ${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 具体的提示符,按分类含义如下: 主要信息: \u 当前 ...
- 【模板】Tarjan缩点
洛谷3387 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...
- java 同时安装多版本问题
java 同时安装多版本问题(转) http://www.cnblogs.com/SamuelSun/p/6022296.html http://blog.csdn.net/u013256622/ar ...
- 用sqlldr导入csv文件
1.新建文件test.ctl,内容如下 load dataCHARACTERSET 'UTF16' \*指定编码格式,很重要*\ infile 'vodall.csv' append into tab ...
- MYSQL 运维
http://www.eimhe.com/thread-142990-1-1.html http://www.eimhe.com/search.php?mod=forum&searchid=6 ...
- 装饰器 转载自 http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html
今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数 ...
- php session自定义处理
原文:http://www.cnblogs.com/mrcoke/ 这个人的博客上转的. 这个博客也好: 学算法和数据结构!!http://blog.csdn.net/21aspnet/articl ...
- [React Storybook] Get started with Storybook for React
Storybook is a UI component development environment for React, Vue, and Angular. With that, you can ...
- 改动mysqlpassword
1.假设没有password,则 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); ...