用java写个算法怎样把薪水相同的人的名字打印出来,如下表
Name Salary
a 2000
b 1500
c 2000
d 3000
e 1300
f 1500

解决方案 »

  1.   

    String[] name={"a","b","c","d","e","f"};
    int[] salary={2000,1500,2000,3000,1300,1500};
    boolean[] flag={true,true,true,true,true,true};for(int i=0;i<6;i++){   if(!flag[i]) continue;    //判断是否已比较过
        System.out.println("薪水为 "+salary[i]+" 如下:");
       for(int j=i;j<6;j++){
          if(salary[i]==salary[j]&&flag[j]){
             System.out.print(name[j]+" ");
             flag[j]=false;      //已经比较过的,就输出,并把标志设为“false“
          }
       }
       System.out.println("");
    }
            
    现在在网吧,所以这程序无法验证;
    大概思路就这样,
    你自个再做相应的调整吧
      

  2.   

    用 hashmap 来遍历吧,get出来为null 就新增进去,否则追加
      

  3.   

    Test.javaimport java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;public class Test {
    public static void main(String[] args) {
    List<Employee> list = new ArrayList<Employee>();
    list.add(new Employee("a", 2000));
    list.add(new Employee("b", 1500));
    list.add(new Employee("c", 2000));
    list.add(new Employee("d", 3000));
    list.add(new Employee("e", 1300));
    list.add(new Employee("f", 1500));
    Collections.sort(list, new Comparator<Employee>() {
    public int compare(Employee o1, Employee o2) {
    int salary1 = o1.getSalary();
    int salary2 = o2.getSalary();
    if (salary1 == salary2) {
    System.out.println(o1.getName() + "\t和\t" + o2.getName() +"\t的工资一样,为:\t" + o1.getSalary());
    }
    return salary1 > salary2 ? 1 : salary1 < salary2 ? -1 : 0;
    }
    });
    }
    }
    Employee.javapublic class Employee {
    private String name; private int salary; public Employee() {
    super();
    } public Employee(String name, int salary) {
    super();
    this.name = name;
    this.salary = salary;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getSalary() {
    return salary;
    } public void setSalary(int salary) {
    this.salary = salary;
    }}
      

  4.   

    select distinct(salary) from tablename
    得到resultset
    Map<Integer, List> map = new HashMap<Integer, List>();
    while(rs.next){
       int salaryVal = rs.getInt(1);
       执行 select * from tablename where salary = salaryVal 得到resultset2
       List用来保存同样公资所有人的姓名列表
        map.put(salaryVal, List);
    }
      

  5.   

    遍历输入的数据,然后使用一个Map<Integer,List>对象来保存分类结果,
    Integer表示的是salary,List中存放的是具有salary的人名.
    在分组完成后迭代这个Map对象,将对应的List中的数据打印出来就OK了.这是思路.保证速度和正确性.