zoukankan      html  css  js  c++  java
  • 软件设计迭代器模式

    1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从小到大和从大到小两种次序输出学生信息。

    package 实验18迭代器模式;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    /**
     * 通过实现comparable接口来比较多个对象的属性值,并按顺序输出
     * @author young
     *
     */
    class Student implements Comparable<Student> {
        private int id;
        private String name;
        private int age;
        
         
        public Student(int id, String name, int age) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
     
        }
     
        // 三个返回结果都要写出来
        public int compareTo(Student o) {
            if(this.id > o.id){
                return -1;
            }else if(this.id < o.id){
                return 1;
            }else {
                return 0;
            }
        }
        public int compareTo2(Student o) {
            if(this.id < o.id){
                return -1;
            }else if(this.id > o.id){
                return 1;
            }else {
                return 0;
            }
        }
        @Override
        public String toString(){
            return "姓名: " + this.name + ". 学号: " + this.id + ". 年龄: " + this.age
                     ;
        }
    }
    Student
    package 实验18迭代器模式;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class ComparableTest1 {
        public static void main(String[] args) {
            Student s1 = new Student(1, "张三", 50);
            Student s2 = new Student(3, "李1", 30 );
            Student s3 = new Student(5, "王2", 29 );
            Student s4 = new Student(5, "高3", 34 );
            Student s5 = new Student(6, "高3", 34 );
            Student s6 = new Student(58, "高3", 34 );
            Student s7 = new Student(69, "高3", 34 );
            Student s8 = new Student(86, "高3", 34 );
            Student s9 = new Student(28, "高3", 34 );
            Student s10 = new Student(68, "高3", 34 );
            Student s11= new Student(354, "高3", 34 );
            Student s12 = new Student(24, "高3", 34 );
            Student s13= new Student(768, "高3", 34 );
            Student s14 = new Student(270, "高3", 34 );
            Student s15 = new Student(767, "高3", 34 );
            Student s16 = new Student(77, "高3", 34 );
            Student s17 = new Student(78, "高3", 34 );
            Student s18 = new Student(80, "高3", 34 );
            Student s19 = new Student(81, "高3", 34 );
            Student s20 = new Student(82, "高3", 34 );
            Student s21 = new Student(83, "高3", 34 );
            Student s22 = new Student(84, "高3", 34 );
            Student s23 = new Student(85, "高3", 34 );
            Student s24 = new Student(86, "高3", 34 );
            Student s25 = new Student(87, "高3", 34 );
            Student s26 = new Student(88, "高3", 34 );
            Student s27 = new Student(89, "高3", 34 );
            Student s28 = new Student(90, "高3", 34 );
            Student s29 = new Student(91, "高3", 34 );
            Student s30 = new Student(29, "高3", 34 );
            Student s31 = new Student(92, "高3", 34 );
            Student s32 = new Student(93, "高3", 34 );
            Student s33 = new Student(94, "高3", 34 );
            Student s34 = new Student(95, "高3", 34 );
            Student s35 = new Student(96, "高3", 34 );
            Student s36 = new Student(97, "高3", 34 );
            Student s37 = new Student(98, "高3", 34 );
            Student s38 = new Student(99, "高3", 34 );
            Student s39 = new Student(100, "高3", 34 );
            Student s40 = new Student(101, "高3", 34 );
            Student s41 = new Student(102, "高3", 34 );
            Student s42 = new Student(103, "高3", 34 );
            Student s43 = new Student(104, "高3", 34 );
            Student s44 = new Student(105, "高3", 34 );
            
            
            List<Student> list = new ArrayList<Student>();
            list.add(s1);
            list.add(s2);
            list.add(s3);
            list.add(s4);
            list.add(s5);
            list.add(s6);
            list.add(s7);
            list.add(s8);
            list.add(s9);
            list.add(s10);
            list.add(s11);
            list.add(s12);
            list.add(s13);
            list.add(s14);
            list.add(s15);
            list.add(s16);
            list.add(s17);
            list.add(s18);
            list.add(s19);
            list.add(s20);
            list.add(s21);
            list.add(s22);
            list.add(s23);
            list.add(s24);
            list.add(s25);
            list.add(s26);
            list.add(s27);
            list.add(s28);
            list.add(s29);
            list.add(s30);
            list.add(s31);
            list.add(s32);
            list.add(s33);
            list.add(s34);
            list.add(s35);
            list.add(s36);
            list.add(s37);
            list.add(s38);
            list.add(s39);
            list.add(s40);
            list.add(s41);
            list.add(s42);
            list.add(s43);
            list.add(s44);
          
            Collections.sort(list);
            System.out.println("按照学号从大到小进行排序输出: ");
            for(Student stu : list){
                System.out.println(stu.toString());
            }
        }
    }
    ComparableTest1
  • 相关阅读:
    【转】【SEE】基于SSE指令集的程序设计简介
    【转】【Asp.Net】asp.net服务器控件创建
    ControlTemplate in WPF ——ScrollBar
    ControlTemplate in WPF —— Menu
    ControlTemplate in WPF —— Expander
    ControlTemplate in WPF —— TreeView
    ControlTemplate in WPF —— ListBox
    ControlTemplate in WPF —— ComboBox
    ControlTemplate in WPF —— TextBox
    ControlTemplate in WPF —— RadioButton
  • 原文地址:https://www.cnblogs.com/feng747/p/15574862.html
Copyright © 2011-2022 走看看