這個是報道當天考試題目是老師隨意給你一張試卷有三道題有簡有難全憑天意
1.選擇排序 和冒泡排序
2.給定一個字符串統計每一個字母出現的次數
3.把一個文件夾複製到另外一個路徑下java
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把字符串反轉app
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.有兩個儲戶每次存一百 存三次用多線程寫ide
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.給一個小數去除小數點後幾位(能夠考慮用正則作)this
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.去除重複元素 若是年齡和姓名相同則爲重複spa
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求兩個字符串交集最長的字符code
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");
... }
若是老師給你的題兩題以上看不懂直接給老師說換題