合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 2018-03-21 周测试题 **机测:** 在 Eclipse 中新建项目,将以下试题完成,时间 60 min 1、将字符串「a-b-c-d-e-f」按 「-」 切割,找到 「c」字符,替换为大写,然后**倒序**输出 「f-e-d-C-b-a」。 ~~~ package com.ntqn.test.title; public class Test1 { public static void main(String[] args) { String str = "a-b-c-d-e-f"; String[] strs = str.split("-"); // 循环遍历,小写字母变大写 for (int i = 0; i < strs.length; i++) { String s = strs[i]; if (s.equals("c")) { strs[i] = s.toUpperCase(); } } String temp = ""; for (int i = strs.length - 1; i >= 0; i--) { temp += strs[i] + "-"; } // 去除最后一个短横 System.out.println(temp.substring(0, temp.length()-1)); } } ~~~ 2、定义学生类(包含学号、姓名、年龄),将你所在小组组员添加到一个集合中,并按**学号排序**后输出。 ~~~ package com.ntqn.test.title; public class Student implements Comparable<Student> { private int code; private String name; private int age; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(int code, String name, int age) { super(); this.code = code; this.name = name; this.age = age; } @Override public int compareTo(Student o) { int flag; if (this.code > o.code) { flag = -1; } else if (this.code == o.code) { flag = 0; } else { flag = 1; } return flag; } @Override public String toString() { return "Student [code=" + code + ", name=" + name + ", age=" + age + "]"; } } ~~~ ~~~ package com.ntqn.test.title; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test2 { public static void main(String[] args) { List<Student> teams = new ArrayList<Student>(); // 新建学生对象 Student stu1 = new Student(1, "Tom", 15); Student stu2 = new Student(3, "Jack", 13); Student stu3 = new Student(2, "Helen", 18); Student stu4 = new Student(4, "May", 12); // 向集合添加学生对象 teams.add(stu1); teams.add(stu2); teams.add(stu3); teams.add(stu4); // 按学号倒叙排列 Collections.sort(teams); // 遍历学生 for (Student stu : teams) { System.out.println(stu); } } } ~~~ 3、紧接第二题,用**单例设计**一个服务类,并定义一个方法,可以**随机抽取**集合中的某个学生对象,并打印输出。 ~~~ package com.ntqn.test.title; import java.util.List; public class Service { private static Service service = new Service(); private Service() { } public static Service getInstance() { return service; } public Student randomStu(List<Student> stus) { if (null != stus && !stus.isEmpty()) { return stus.get((int)(Math.random() * stus.size())); } else { return null; } } } ~~~ ~~~ package com.ntqn.test.title; import java.util.ArrayList; import java.util.List; public class Test3 { public static void main(String[] args) { List<Student> teams = new ArrayList<Student>(); Student stu1 = new Student(1, "Tom", 15); Student stu2 = new Student(3, "Jack", 13); Student stu3 = new Student(2, "Helen", 18); Student stu4 = new Student(4, "May", 12); teams.add(stu1); teams.add(stu2); teams.add(stu3); teams.add(stu4); // 通过静态方法获取对象 Service service = Service.getInstance(); System.out.println("==========随机抽取学生============="); Student randomStu = service.randomStu(teams); System.out.println(randomStu); } } ~~~