黑马面试题
这个是报道当天考试题目是老师随意给你一张试卷有三道题有简有难全凭天意
1.选择排序 和冒泡排序
2.给定一个字符串统计每个字母出现的次数
3.把一个文件夹复制到另一个路径下
public class Test3 { private static File des; private static File src; public static void main(String[] args) { src = new File("c:\\src"); des = new File("c:\\des"); listFile(src,des); System.out.println("拷贝完成"); } public static void listFile(File f,File parent){ if(f.isFile()){ copyFile(f,parent); }else{ File p = new File(parent,f.getName()); System.out.println(f.getName()); p.mkdir(); File[] listFiles = f.listFiles(); for (File file : listFiles) { listFile(file,p); } } } private static void copyFile(File src,File parent) { try { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(new File(parent,src.getName())); int len = 0; byte[] buf = new byte[1024]; while((len=fis.read(buf))!=-1){ fos.write(buf,0,len); } fos.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } } }
4.定义一个二维数组遍历求最小值
5.解决异常的方法
分为三种 try catch
throw throws
6.打印99乘法表
7写一个死锁
class Zhangsan{ // 定义张三类 public void say(){ System.out.println("张三对李四说:“你给我画,我就把书给你。”") ; } public void get(){ System.out.println("张三得到画了。") ; } }; class Lisi{ // 定义李四类 public void say(){ System.out.println("李四对张三说:“你给我书,我就把画给你”") ; } public void get(){ System.out.println("李四得到书了。") ; } }; public class DeadLock implements Runnable { private static Zhangsan zs = new Zhangsan(); private static Lisi ls = new Lisi(); private boolean flag =false; // 用于判断哪个对象先执行 @Override public void run() { if (flag) { synchronized (zs) { zs.say(); synchronized (ls) { //同步第二个对象 zs.get(); } } } else { synchronized (ls) { ls.say(); synchronized (zs) { ls.get(); } } } } public static void main(String[] args) { DeadLock dl1 = new DeadLock(); DeadLock dl2 = new DeadLock(); dl1.flag = true; dl2.flag = false; Thread t1 = new Thread(dl1); Thread t2 = new Thread(dl2); t1.start(); t2.start(); } }
8把字符串反转
public class Test8 { public static void main(String[] args) { String str = "abcdef"; char[] chars = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (char c : chars) { sb.append(c); } sb.reverse(); System.out.println(sb.toString()); } }
9.有两个储户每次存一百 存三次用多线程写
class Save implements Runnable{ int money = 0; @Override public void run() { for (int i = 0; i < 3; i++) { money+=100; System.out.println(Thread.currentThread().getName()+":"+money); } } } public class Test9{ public static void main(String[] args) { Thread t1 = new Thread(new Save()); Thread t2 = new Thread(new Save()); t1.start(); t2.start(); } }
10.给一个小数去除小数点后几位(可以考虑用正则做)
public class Test10{ public static void main(String[] args) { float num = 123.123456f; int count = 2; String numStr = String.valueOf(num); int index = numStr.lastIndexOf(".")+count; String substring = numStr.substring(0,index+1); System.out.println(substring); } }
11.去除重复元素 如果年龄和姓名相同则为重复
import java.util.ArrayList; import java.util.List; class Person { private String name; private int age; 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 Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } @Override public boolean equals(Object obj) { Person p = (Person) obj; if (this.age == p.age && this.name.equals(p.name)) { return true; } return false; } } public class Test9 { public static void main(String[] args) { List<Person> list = new ArrayList<Person>(); list.add(new Person("zhangsan", 23)); list.add(new Person("zhangsan", 23)); list.add(new Person("zhangsan", 25)); list.add(new Person("lisi", 25)); removeDuplicate(list); } public static void removeDuplicate(List list) { for (int i = 0; i < list.size() ; i++) { for (int j = i+1; j < list.size(); j++) { if (list.get(j).equals(list.get(i))) { list.remove(j); } } } System.out.println(list); } }
12.定义一个ArrayList 按年龄大小排序
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Person implements Comparable<Person> { private String name; private int age; 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 Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } @Override public boolean equals(Object obj) { Person p = (Person) obj; if (this.age == p.age && this.name.equals(p.name)) { return true; } return false; } @Override public int compareTo(Person o) { return this.age - o.age; } } public class Test9 { public static void main(String[] args) { List<Person> list = new ArrayList<Person>(); list.add(new Person("zhangsan", 23)); list.add(new Person("zhangsan1", 22)); list.add(new Person("zhangsan", 25)); list.add(new Person("lisi", 25)); Collections.sort(list); for (Person person : list) { System.out.println(person); } } }
13求两个字符串交集最长的字符
public class BigestCommonSubString { // 查找两个字符串的最大相同子串,最大交集 public static void main(String[] args) { String str1 = "12345612"; String str2 = "1256345612"; String targetString = getSubString(str1, str2); System.out.println(targetString); } // 获得最大相同子串,这个方法可以被外部调用 public static String getSubString(String str1, String str2) { String targetString = null; // 取出其中较短的字符串(照顾效率) String shorter = str1.length() > str2.length() ? str2 : str1; String longer = shorter.equals(str1) ? str2 : str1; // 在较短的字符串中抽取其‘所有长度’的子串,顺序由长到短 for(int subLength = shorter.length(); subLength > 0; subLength--){ // 子串的起始角标由 0 开始右移,直至子串尾部与母串的尾部-重合为止 for(int i = 0; i+subLength <= shorter.length(); i++){ String subString = shorter.substring(i, i+subLength); // 取子串 if(longer.contains(subString)){ // 注意 ‘=’ return subString; } } } return null; } }
14将d:\\目录下的所有.java文件复制d:\\jad目录下原文件扩展名改为.jad
String oldName = src.getName(); if (oldName.contains(".java")) { oldName = oldName.replace(".java", ".jad");
... }
如果老师给你的题两题以上看不懂直接给老师说换题
posted on 2014-11-23 19:03 cherry_himi 阅读(459) 评论(0) 编辑 收藏 举报