java集合——题4,6

4、List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: 

List list = new ArrayList(); 

list.add(“Hello”); 

list.add(“World”); 

list.add(“Learn”); //此时list 为Hello World Learn 

reverseList(list); //调用reverseList 方法之后,list 为Learn World Hello

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.JiHe.test.Person;

public class DaoXu {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        List<String> list =new ArrayList<>();
        List<String> reverselist=new ArrayList<>(3);
        list.add("Hello\t");
        list.add("World\t");
        list.add("Learn\t");
        System.out.print("list= ");
        for(String str:list){
            System.out.print(str);
        }
        System.out.println();
        for(int i=list.size()-1;i>=0;i--){
            reverselist.add(list.get(i));
        }
        System.out.print("reverselist= ");
        for(String str:reverselist){
            System.out.print(str);
        }
    }

}

6、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 Work {
    private int age;
    private String name;
    private double salary;
    public Work(){
        
    }
    public Work(String name,int age,double salary){
        this.name=name;
        this.age=age;
        this.salary=salary;
    }
    public void work(){
        System.out.println(name+" work");
    }
    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 "Work [Age=" + getAge() + ", Name=" + getName() + ", Salary=" + getSalary() + "]";
    }
    public static void main(String[] args){
        List<Work> list = new ArrayList<>();
        list.add(new Work("zhang3",18,3000));
        list.add(new Work("li4",25,3500));
        list.add(new Work("wang5",22,3200));
        list.add(1,new Work("zhao6",24,3300));
        list.remove(3);
        for(Work w:list){
            System.out.println(w);
        }
        Iterator<Work> it =list.iterator();
        while(it.hasNext()){
            it.next().work();
        }
    }
}

 

posted @ 2016-09-29 10:13  荆小八  阅读(395)  评论(0编辑  收藏  举报