1、编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,
a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
FileManager a = new FileManager("a.txt", new char[] { '\n' });
FileManager b = new FileManager("b.txt", new char[] { '\n', ' ' });
FileWriter c = new FileWriter("c.txt");
String aWord = null;
String bWord = null;
while ((aWord = a.nextWord()) != null) {
c.write(aWord + "\n");
bWord = b.nextWord();
if (bWord != null)
c.write(bWord + "\n");
}
while ((bWord = b.nextWord()) != null) {
c.write(bWord + "\n");
}
c.close();
}
}
class FileManager {
String[] words = null;
int pos = 0;
public FileManager(String filename, char[] seperators) throws Exception {
File f = new File(filename);
FileReader reader = new FileReader(f);
char[] buf = new char[(int) f.length()];
int len = reader.read(buf);
String results = new String(buf, 0, len);
String regex = null;
if (seperators.length > 1) {
regex = "" + seperators[0] + "|" + seperators[1];
} else {
regex = "" + seperators[0];
}
words = results.split(regex);
}
public String nextWord() {
if (pos == words.length)
return null;
return words[pos++];
}
}
2、编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。
答:listFiles方法接受一个FileFilter对象,这个FileFilter对象就是过虑的策略对象,
不同的人提供不同的FileFilter实现,即提供了不同的过滤策略。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;
public class Jad2Java {
public static void main(String[] args) throws Exception {
File srcDir = new File("java");
if (!(srcDir.exists() && srcDir.isDirectory()))
throw newException("目录不存在");
File[] files = srcDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".java");
}
});
System.out.println(files.length);
File destDir = new File("jad");
if (!destDir.exists())
destDir.mkdir();
for (File f : files) {
FileInputStream fis = new FileInputStream(f);
String destFileName = f.getName().replaceAll("\\.java$", ".jad");
FileOutputStream fos = new FileOutputStream(new File(destDir, destFileName));
copy(fis, fos);
fis.close();
fos.close();
}
}
private static void copy(InputStream ips, OutputStream ops) throws Exception {
int len = 0;
byte[] buf = new byte[1024];
while ((len = ips.read(buf)) != -1) {
ops.write(buf, 0, len);
}
}
}
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
class jad2java {
File srcDir =new File("d:\\java");
File[] files = srcDir.listFiles(new MyFileFilter());
class MyFileFilter implememyts FileFilter{
public boolean accept(Filepathname){
returnpathname.getName().endsWith(".java");
}
File[] listFiles(FileFilter filter) {
File[] files = listFiles(filter);
File[] acceptedFiles = new File[files.length];
int pos = 0;
for (File file : files) {
boolean accepted = filter.accept(file);
if (accepted) {
acceptedFiles[pos++] = file;
}
}
Arrays.copyOf(acceptedFiles, pos);
}
}
3、编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个,
如“我ABC”,4,应该截取“我AB”,
输入“我ABC汉DEF”,6,应该输出“我ABC”,而不是“我ABC+汉的半个”。
答:
首先要了解中文字符有多种编码及各种编码的特征。
假设n为要截取的字节数。
public class jad2Java {
public static void main(String[] args)throws Exception{
String str = "我a爱中华abc我爱荣耀def";
int num =trimGBK(str.getBytes("GBK"),5);
System.out.println(str.substring(0,num));
}
public static int trimGBK(byte[] buf, int n) {
int num = 0;
boolean bChineseFirstHalf = false;
for (int i = 0; i < n; i++) {
if (buf[i] < 0 && !bChineseFirstHalf) {
bChineseFirstHalf = true;
} else {
num++;
bChineseFirstHalf = false;
}
}
return num;
}
}
4、有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。
答:哈哈,其实包含中文字符、英文字符、数字字符原来是出题者放的烟雾弹。
String content = "中国aadf的111萨bbb菲的zz萨菲";
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
Integer num = (Integer) map.get(c);
if (num == null)
num = 1;
else
num = num + 1;
map.put(c, num);
}
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
估计是当初面试的那个学员表述不清楚,问题很可能是:
如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,
假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。
int engishCount = 0;
int chineseCount = 0;
int digitCount = 0;
String str = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= '0' && ch <= '9') {
digitCount++;
} else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
engishCount++;
} else {
chineseCount++;
}
}
System.out.println("………");
}
5、说明生活中遇到的二叉树,用java实现二叉树
这是组合设计模式。
我有很多个(假设10万个)数据要保存起来,以后还需要从保存的这些数据中检索是否存在某个数据,
(我想说出二叉树的好处,该怎么说呢?那就是说别人的缺点),假如存在数组中,那么,碰巧要找的数字位于99999那个地方,
那查找的速度将很慢,因为要从第1个依次往后取,取出来后进行比较。
平衡二叉树(构建平衡二叉树需要先排序,我们这里就不作考虑了)
可以很好地解决这个问题,但二叉树的遍历(前序,中序,后序)效率要比数组低很多,原理如下图:
代码如下:
public class Node {
public int value;
public Node left;
public Node right;
public void store(int value) {
if (value < this.value) {
if (left == null) {
left = new Node();
left.value = value;
} else {
left.store(value);
}
} else if (value > this.value) {
if (right == null) {
right = new Node();
right.value = value;
} else {
right.store(value);
}
}
}
public boolean find(int value) {
System.out.println("happen " + this.value);
if (value == this.value) {
return true;
} else if (value > this.value) {
if (right == null)
return false;
return right.find(value);
} else {
if (left == null)
return false;
return left.find(value);
}
}
public void preList() {
System.out.print(this.value + ",");
if (left != null)
left.preList();
if (right != null)
right.preList();
}
public void middleList() {
if (left != null)
left.preList();
System.out.print(this.value + ",");
if (right != null)
right.preList();
}
public void afterList() {
if (left != null)
left.preList();
if (right != null)
right.preList();
System.out.print(this.value + ",");
}
public static void main(String[] args) {
int[] data = new int[20];
for (int i = 0; i < data.length; i++) {
data[i] = (int) (Math.random() * 100) + 1;
System.out.print(data[i] + ",");
}
System.out.println();
Node root = new Node();
root.value = data[0];
for (int i = 1; i < data.length; i++) {
root.store(data[i]);
}
root.find(data[19]);
root.preList();
System.out.println();
root.middleList();
System.out.println();
root.afterList();
}
}
-----------------又一次临场写的代码---------------------------
import java.util.Arrays;
import java.util.Iterator;
public class Node {
private Node left;
private Node right;
private int value;
public Node(int value) {
this.value = value;
}
public void add(int value) {
if (value > this.value) {
if (right != null)
right.add(value);
else {
Node node = new Node(value);
right = node;
}
} else {
if (left != null)
left.add(value);
else {
Node node = new Node(value);
left = node;
}
}
}
public boolean find(int value) {
if (value == this.value)
return true;
else if (value > this.value) {
if (right == null)
return false;
else
return right.find(value);
} else {
if (left == null)
return false;
else
return left.find(value);
}
}
public void display() {
System.out.println(value);
if (left != null)
left.display();
if (right != null)
right.display();
}
public static void main(String[] args) {
int[] values = new int[8];
for (int i = 0; i < 8; i++) {
int num = (int) (Math.random() * 15);
if (!contains(values, num))
values[i] = num;
else
i--;
}
System.out.println(Arrays.toString(values));
Node root = new Node(values[0]);
for (int i = 1; i < values.length; i++) {
root.add(values[i]);
}
System.out.println(root.find(13));
root.display();
}
public static boolean contains(int[] arr, int value) {
int i = 0;
for (; i < arr.length; i++) {
if (arr[i] == value)
return true;
}
return false;
}
}
6、从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序:
1,张三,28
2,李四,35
3,张三,28
4,王五,35
5,张三,28
6,李四,35
7,赵六,28
8,田七,35
程序代码如下(答题要博得用人单位的喜欢,包名用该公司,面试前就提前查好该公司的网址,如果查不到,现场问也是可以的。还要加上实现思路的注释):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
public class GetNameTest {
public static void main(String[] args) {
Map results = new HashMap();
InputStream ips = GetNameTest.class.getResourceAsStream("info.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(ips));
String line = null;
try {
while ((line = in.readLine()) != null) {
dealLine(line, results);
}
sortResults(results);
} catch (IOException e) {
e.printStackTrace();
}
}
static class User {
public String name;
public Integer value;
public User(String name, Integer value) {
this.name = name;
this.value = value;
}
@Override
public boolean equals(Object obj) {
boolean result = super.equals(obj);
System.out.println(result);
return result;
}
}
private static void sortResults(Map results) {
TreeSet sortedResults = new TreeSet(new Comparator() {
public int compare(Object o1, Object o2) {
User user1 = (User) o1;
User user2 = (User) o2;
if (user1.value < user2.value) {
return -1;
} else if (user1.value > user2.value) {
return 1;
} else {
return user1.name.compareTo(user2.name);
}
}
});
Iterator iterator = results.keySet().iterator();
while (iterator.hasNext()) {
String name = (String) iterator.next();
Integer value = (Integer) results.get(name);
if (value > 1) {
sortedResults.add(new User(name, value));
}
}
printResults(sortedResults);
}
private static void printResults(TreeSet sortedResults) {
Iterator iterator = sortedResults.iterator();
while (iterator.hasNext()) {
User user = (User) iterator.next();
System.out.println(user.name + ":" + user.value);
}
}
public static void dealLine(String line, Map map) {
if (!"".equals(line.trim())) {
String[] results = line.split(",");
if (results.length == 3) {
String name = results[1];
Integer value = (Integer) map.get(name);
if (value == null)
value = 0;
map.put(name, value + 1);
}
}
}
}
7、写一个Singleton出来。
第一种:饱汉模式
public class SingleTon {
private SingleTon() {}
private final static SingleTon instance = new SingleTon();
public static SingleTon getInstance() {
return instance;
}
}
第二种:饥汉模式
public class SingleTon {
private SingleTon() {}
private static SingleTon instance = null;
public static synchronized SingleTon getInstance(){
if(instance == null)
instance = new SingleTon();
return instance;
}
}
第三种:用枚举
public enum SingleTon {
ONE;
}
第三:更实际的应用(在什么情况用单例)
public class SingleTon {
private int count = 0;
public synchronized int getSequence() {
return ++count;
}
private SingleTon() {}
private final static SingleTon instance = new SingleTon();
public static SingleTon getInstance() {
return instance;
}
}
第四:
public class MemoryDao{
private HashMap map = new HashMap();
public void add(Student stu1) {
map.put(SequenceGenerator.getInstance().getSequence(), stu1);
}
}
Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。
一般Singleton模式通常有几种种形式:
第一种形式: 定义一个类,它的构造函数为private的,它有一个static的private的该类变量,
在类初始化时实例话,通过一个public的getInstance方法获取对它的引用,继而调用其中的方法。
public class Singleton {
private Singleton() {}
private static Singleton instance = new Singleton();
public static Singleton getInstance() {
return instance;
}
}
第二种形式:
public class Singleton {
private static Singleton instance = null;
public static synchronized Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}
其他形式:
定义一个类,它的构造函数为private的,所有方法为static的。
一般认为第一种形式要更加安全些
8、递归算法题1
一个整数,大于0,不用循环和本地变量,按照n,2n,4n,8n的顺序递增,当值大于5000时,把值按照指定顺序输出来。
例:n=1237
则输出为:
1237,
2474,
4948,
9896,
9896,
4948,
2474,
1237,
提示:写程序时,先致谢按递增方式的代码,写好递增的以后,再增加考虑递减部分。
public static void doubleNum(int n){
System.out.println(n);
if(n<=5000)
doubleNum(n*2);
System.out.println(n);
}
9、递归算法题2
第1个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
import java.util.Date;
public class A1 {
public static void main(String[] args) {
System.out.println(computeAge(8));
}
public static int computeAge(int n) {
if (n == 1)
return 10;
return computeAge(n - 1) + 2;
}
public static void toBinary(int n, StringBuffer result) {
if (n / 2 != 0)
toBinary(n / 2, result);
result.append(n % 2);
}
}
10、排序都有哪几种方法?请列举。用JAVA实现一个快速排序。
本人只研究过冒泡排序、选择排序和快速排序,下面是快速排序的代码:
public class QuickSort {
public void quickSort(String[] strDate, int left, int right) {
String middle, tempDate;
int i, j;
i = left;
j = right;
middle = strDate[(i + j) / 2];
do {
while (strDate[i].compareTo(middle) < 0 && i < right)
i++;
while (strDate[j].compareTo(middle) > 0 && j > left)
j--;
if (i <= j) {
tempDate = strDate[i];
strDate[i] = strDate[j];
strDate[j] = tempDate;
i++;
j--;
}
} while (i <= j);
if (i < right) {
quickSort(strDate, i, right);
}
if (j > left) {
quickSort(strDate, left, j);
}
}
public static void main(String[] args) {
String[] strVoid = new String[] { "11", "66", "22", "0", "55", "22", "0", "32" };
QuickSort sort = new QuickSort();
sort.quickSort(strVoid, 0, strVoid.length - 1);
for (int i = 0; i < strVoid.length; i++) {
System.out.println(strVoid[i] + " ");
}
}
}
11、有数组a[n],用java代码将数组元素顺序颠倒
import java.util.Arrays;
public class SwapDemo {
public static void main(String[] args) {
int[] a = new int[] {
(int) (Math.random() * 1000),
(int) (Math.random() * 1000),
(int) (Math.random() * 1000),
(int) (Math.random() * 1000),
(int) (Math.random() * 1000)
};
System.out.println(a);
System.out.println(Arrays.toString(a));
swap(a);
System.out.println(Arrays.toString(a));
}
public static void swap(int a[]) {
int len = a.length;
for (int i = 0; i < len / 2; i++) {
int tmp = a[i];
a[i] = a[len - 1 - i];
a[len - 1 - i] = tmp;
}
}
}
12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。
去零的代码:
return sb.reverse().toString()
.replaceAll("零[拾佰仟]","零")
.replaceAll("零+万","万")
.replaceAll("零+元","元")
.replaceAll("零+","零");
public class RenMingBi {
private static final char[] data = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
private static final char[] units = new char[] { '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿' };
public static void main(String[] args) {
System.out.println(convert(135689123));
}
public static String convert(int money) {
StringBuffer sbf = new StringBuffer();
int unit = 0;
while (money != 0) {
sbf.insert(0, units[unit++]);
int number = money % 10;
sbf.insert(0, data[number]);
money /= 10;
}
return sbf.toString();
}
}