悠悠隐于市 2010-05-18 23:16
浏览 374
已采纳

Java中关于List集合输出问题?

我一个List类型的集合中有以下数据:
Student类中的属性有:
Company ,No1,NO2,NO3,NO4;
省略getter。setter.

输出的结果是:
Company No1 No2 No3 No4

WTCCNDEPT1 0 0 2 1
WTCCNDEPT1 0 0 3 1
WTCCNDEPT1 0 0 2 1
WTCCNDEPT2 0 0 3 2
WTCCNDEPT2 0 0 3 2

这种输出来.有重复的数据..我想要的结果是:
//如果重复;则把重复的行变成一行;然后把NO1, NO2,NO3,NO4 的列的值累加.

还有一种情况就是;两行的值,完全相通,则移除一行.;

Company No1 No2 No3 No4
WTCCNDEPT1 0 0 7 3
WTCCNDEPT2 0 0 3 2

我自己没有做出来.. 求大家帮下忙..能不能帮我想想办法..
解决这个问题..谢谢你们拉..

  • 写回答

3条回答 默认 最新

  • weixin_42518082 2010-05-19 08:48
    关注

    [code="java"]
    public static void main(String[] args) throws Exception {
    List students = new ArrayList();
    students.add(new Student("WTCCNDEPT1", 0, 0, 2, 1));
    students.add(new Student("WTCCNDEPT1", 0, 0, 3, 1));
    students.add(new Student("WTCCNDEPT1", 0, 0, 2, 1));
    students.add(new Student("WTCCNDEPT2", 0, 0, 3, 2));
    students.add(new Student("WTCCNDEPT2", 0, 0, 3, 2));

    Set studsSet = new HashSet(students);

    List newStuds = new ArrayList();

    for (Student student : studsSet) {
    String company = student.getCompany();
    boolean found = false;
    for (Student stud : newStuds) {
    if (company.equals(stud.getCompany())) {
    stud.setNo1(stud.getNo1() + student.getNo1());
    stud.setNo2(stud.getNo2() + student.getNo2());
    stud.setNo3(stud.getNo3() + student.getNo3());
    stud.setNo4(stud.getNo4() + student.getNo4());
    found = true;
    break;
    }
    }
    if (!found) {
    newStuds.add(student);
    }
    }
    for (Student student : newStuds) {
    System.out.println(student);
    }
    }
    [/code]
    [code="java"]

    public class Student{
    private String company;
    private int no1;
    private int no2;
    private int no3;
    private int no4;

    public Student(String company, int no1, int no2, int no3, int no4) {
    this.company = company;
    this.no1 = no1;
    this.no2 = no2;
    this.no3 = no3;
    this.no4 = no4;
    }

    // 省略 getter和setter

    @Override
    public int hashCode() {
    int hash = company.hashCode();
    hash = hash * 37 + no1;
    hash = hash * 37 + no2;
    hash = hash * 37 + no3;
    hash = hash * 37 + no4;
    return hash;
    }

    @Override
    public boolean equals(Object obj) {
    if (this == obj) {
    return true;
    }
    if (obj instanceof Student) {
    Student other = (Student) obj;
    return company.equals(other.getCompany()) && no1 == other.getNo1() && no2 == other.getNo2()
    && no3 == other.getNo3() && no4 == other.getNo4();
    }
    return false;
    }

    @Override
    public String toString() {
    return String.format("%s\t%d\t%d\t%d\t%d", company, no1, no2, no3, no4);
    }
    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号