- public class Main{
- public static void main(String[] args) {
- List<Student> list = new ArrayList<Student>();
- list.add(new Student("WTCCNDEPT1",0,0,2,1));
- list.add(new Student("WTCCNDEPT1",0,0,3,1));
- list.add(new Student("WTCCNDEPT1",0,0,2,1));
- list.add(new Student("WTCCNDEPT2",0,0,3,2));
- list.add(new Student("WTCCNDEPT2",0,0,3,2));
- System.out.println("如果重复,把重复的行变为一行,且No1,No2,No3,No4值累加");
- System.out.println("Company\t\tNo1\tNo2\tNo3\tNo4");
- for(int i=0;i<list.size();i++){
- int j=0;
- Student s = list.get(i);
- for(;j<i;j++){
- if(list.get(i).toString().equals(list.get(j).toString())){//检查前面时候有重复的
- break;//前面有重复的
- }
- }
- if(j==i){//前面没有重复的
- int counts = 1;
- for(j=i+1;j<list.size();j++){
- if(list.get(i).toString().equals(list.get(j).toString())){//检查后面是否有重复的
- counts++;//后面有重复的
- }
- }
- System.out.println(s.getCompany()+"\t"+s.getNo1()*counts+"\t"+s.getNo2()*counts+"\t"+s.getNo3()*counts+"\t"+s.getNo4()*counts);
- }
- }
- System.out.println("两行的值完全相同,就移除一行");
- System.out.println("Company\t\tNo1\tNo2\tNo3\tNo4");
- for(int i=0;i<list.size();i++){
- int j=0;
- for(;j<i;j++){
- if(list.get(i).toString().equals(list.get(j).toString())){
- break;
- }
- }
- if(j==i){
- System.out.println(list.get(i));
- }
- }
- }
- }
- class Student{
- private String company;
- private int no1;
- private int no2;
- private int no3;
- private int no4;
- public Student(String c,int n1,int n2,int n3,int n4){
- this.company = c;
- no1 = n1;
- no2 = n2;
- no3 = n3;
- no4 = n4;
- }
- public String toString(){
- return company+"\t"+no1+"\t"+no2+"\t"+no3+"\t"+no4;
- }
- //get和set方法未贴
- }
复制代码 |