一 选择题
1-5 DDCBC
6-10 BDDCC
二 程序题
1、(5分)判断字符串“上海自来水来自海上”是否为回文并打印,所谓回文是指一个字符序列无论从左向右读 还是从右向左读都是相同的句子。
public class Test01 {
public static void main(String[] args) {
String str = "上海自来水来自海上";
StringBuilder sb = new StringBuilder(str);
StringBuilder reverse = sb.reverse();
System.out.println(str.equals( reverse.toString() ) ? "是回文": "不是回文");
}
}
2、(5分) 编写通用的代码可以查询字符串"Good Good Study, Day Day Up!"中所有"Day"出现的索引位置并打印出来。
public class Test02 {
public static void main(String[] args) {
String str = "Good Good Study, Day Day Up!";
int index = -1;
while ((index = str.indexOf("Day" , (index + 1) ) ) != -1 ){
System.out.println(index);
index += "Day".length() - 1;
}
}
}
3、(5分)产生10个1-100的随机数,并放到一个数组中, 把数组中大于等于10的数字放到一个list集合中,并升序排列打印到控制台。
public class Test03 {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int)(Math.random() * 100 + 1);
}
System.out.println("生成的随即数组:"+Arrays.toString(arr));
List<Integer> list = new ArrayList<>();
for(Integer i : arr){
if (i >= 10){
list.add(i);
}
}
Collections.sort(list);
System.out.println(list);
}
}
4、(10分)定义一个泛型为String类型的List集合,统计该集合中每个字符(注意,不是字符串)出现的次数。例如:集合中有”abc”、”bcd”两个元素,程序最终输出结果为:“a = 1,b = 2,c = 2,d = 1”。
public class Test04 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("abc");
list.add("bcd");
System.out.println(list);
String str = "";
for(String s : list){
str += s;
}
System.out.println(str);
Map<String ,Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
String k = str.charAt(i) + "";
if (map.containsKey(k)){
map.put(k , map.get(k) + 1);
}else{
map.put(k,1);
}
}
System.out.println(map);
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()){
String key = it.next();
Integer value = map.get(key);
System.out.print(key + "=" + value );
if (it.hasNext()){
System.out.print(", ");
}
}
}
}
5、(10分)拷贝一张图片,从一个目录到另外一个目录下(PS:是拷贝是不是移动)
public class Test05 {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("src/com/qiku/day26/1.jpg");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("1_1.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int i = -1;
while ( ( i = bis.read()) != -1){
bos.write(i);
}
bis.close();
bos.close();
}
}
6、(15分)类Student拥有name,age,sex三个属性,请将3个学生的信息存储到文件students.txt中,然后再从文件中读出这3个学生的信息输出到控制台。
public class Test06 {
public static void main(String[] args) throws Exception {
Student stu1 = new Student("张三",20,"男");
Student stu2 = new Student("tom",22,"男");
Student stu3 = new Student("南宫婉",20,"女");
List<Student> list = new ArrayList<>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
FileOutputStream fos = new FileOutputStream("students.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
FileInputStream fis = new FileInputStream("students.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
System.out.println(o);
}
}
class Student implements Serializable{
private String name;
private int age;
private String sex;
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
7、(15分)生成一个长度是3的随机字符串,把这个字符串当作 密码;创建一个破解线程,使用穷举法(就是列举出所有的可能性,别紧张),匹配这个密码;创建一个日志线程,打印都用过哪些字符串去匹配,这个日志线程设计为守护线程;提示: 破解线程把穷举法生成的可能密码放在一个容器中,日志线程不断的从这个容器中拿出可能密码,并打印出来;如果发现容器是空的,就休息1秒,如果发现不是空的,就不停的取出,并打印。
public class Test07 {
public static void main(String[] args) {
String password = generatePassword();
System.out.println("生成的随机密码:" + password);
MatchPassword matchPassword = new MatchPassword(password);
new Thread(matchPassword).start();
Thread logThread = new Thread(new LogThread(matchPassword));
logThread.setDaemon(true);
logThread.start();
}
public static String generatePassword(){
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
String password = "";
for (int i = 0; i < str.length(); i++) {
password = "" + str.charAt( (int)(Math.random() * str.length()) )
+ str.charAt( (int)(Math.random() * str.length()) )
+ str.charAt( (int)(Math.random() * str.length()) ) ;
}
return password;
}
}
class LogThread implements Runnable{
private MatchPassword matchPassword;
public LogThread(MatchPassword matchPassword) {
this.matchPassword = matchPassword;
}
@Override
public void run() {
System.out.println("使用过的密码:");
while (true){
List<String> passwords = matchPassword.getPasswords();
if (passwords.isEmpty()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
synchronized (Object.class){
System.out.println(passwords);
matchPassword.setPasswords(new ArrayList<>());
}
}
}
}
}
class MatchPassword implements Runnable{
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private String password ;
private List<String> passwords ;
public MatchPassword(String password) {
this.password = password;
passwords = new ArrayList<>();
}
@Override
public void run() {
outer : while (true){
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < str.length(); j++) {
for (int k = 0; k < str.length(); k++) {
synchronized (Object.class){
String tryPassword = "" + str.charAt(i) + str.charAt(j) + str.charAt(k);
passwords.add(tryPassword);
if (tryPassword.equals(password)){
System.out.println("密码已破解:" + tryPassword);
break outer;
}
}
}
}
}
}
}
public List<String> getPasswords() {
return passwords;
}
public void setPasswords(List<String> passwords) {
this.passwords = passwords;
}
}
8、(15分)准备一个ArrayList其中存放3000000(三百万个)Hero对象,其名称是随机的, 格式是 hero-[4位随机数]
hero-3229
hero-6232
hero-9365
…
因为总数很大,所以几乎每种都有重复,把名字叫做 hero-5555的所有对象找出来
要求使用两种办法来寻找
1. 不使用HashMap,直接使用for循环找出来,并统计花费的时间
2. 借助HashMap,找出结果,并统计花费的时间
public class Test08 {
public static void main(String[] args) {
ArrayList<Hero> list = new ArrayList<>();
String name ;
for (int i = 0; i < 3000000; i++) {
name = "hero-" + (int)(Math.random() * 9000 + 1000);
list.add(new Hero(name));
}
long start = System.currentTimeMillis();
ArrayList<Hero> hero_5555 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Hero hero = list.get(i);
if (hero.getName().equals("hero-5555")){
hero_5555.add(hero);
}
}
System.out.println(hero_5555);
long end = System.currentTimeMillis();
System.out.println("方式一,共计耗时:" + (end - start) + "ms");
HashMap<String , List<Hero> > map = new HashMap<>();
for (Hero hero : list){
if (map.containsKey(hero.getName())){
map.get(hero.getName()).add(hero);
}else {
ArrayList<Hero> heroes = new ArrayList<>();
heroes.add(hero);
map.put(hero.getName() , heroes );
}
}
start = System.currentTimeMillis();
List<Hero> heroes = map.get("hero-5555");
System.out.println(heroes);
end = System.currentTimeMillis();
System.out.println("方式二,共计耗时:" + (end - start) + "ms");
}
}
class Hero{
private String name;
public Hero(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
|