本文为学习实践的记录,来源于:http://thecodesample.com
package interview;
/*
* 本文给出一个实例,使用递归算法反转字符串
*/
public class StringReversalWithRecursion {
public static void main(String[] args) {
StringReversalWithRecursion test = new StringReversalWithRecursion();
System.out.println(test.reverseString("abc"));
}
public String reverseString(String str) {
if (str.isEmpty()) {
return str;
}
return reverseString(str.substring(1)) + str.charAt(0);
}
}
package interview;
import java.util.Arrays;
/**
* 写一个程序,去除有序数组中的重复数字
* @author xl
*
*/
public class RemoveDuplicateElementsExample {
public static void main(String[] args) {
int[] arr = {1,2,2,3,1,2,3,6,7,6};
int [] out = removeDuplicateDataInArray(arr);
System.out.println(Arrays.toString(out));
}
//移除数组中重复元素
public static int[] removeDuplicateDataInArray(int[] arr){
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("原数组不能为空");
}
int len = arr.length;
if (len == 1) {
return arr;
}
//排序
Arrays.sort(arr);
int start = 0;
int end = 1;
while(end < len) {
if (arr[start] == arr[end]) {
end ++;
} else {
arr[++start] = arr[end++];
}
}
int newLen = start + 1;
int[] newArr = new int[newLen];
System.arraycopy(arr, 0, newArr, 0, newLen);
return newArr;
}
}
package interview;
import java.util.Arrays;
/**
* 合并两个有序数组
*
* @author xl
*
*/
public class MergeTwoSortIntegerArrays {
// 合并有序数组
public static int[] merge(int[] a, int[] b) {
int lena = a.length;
int lenb = b.length;
if (a == null || lena == 0) {
throw new IllegalArgumentException("a数组为空");
}
if (b == null || lenb == 0) {
throw new IllegalArgumentException("b数组为空");
}
int[] newArr = new int[lena + lenb];
System.arraycopy(a, 0, newArr, 0, lena);
System.arraycopy(b, 0, newArr, lena, lenb);
Arrays.sort(newArr); // 方法1
return newArr;
}
public static int[] merge1(int[] a, int[] b) {
if (a == null)
return b;
if (b == null)
return a;
int len1 = a.length;
int len2 = b.length;
int result[] = new int[len1 + len2];
int i = 0;
int j = 0;
int k = 0;
while (i < len1 && j < len2) {
if (a[i] < b[j]) {
result[k++] = a[i];
i++;
} else {
result[k++] = b[j];
j++;
}
}
System.arraycopy(a, i, result, k, (len1 - i));
System.arraycopy(b, j, result, k, (len2 - j));
return result;
}
public static void main(String[] args) {
long t1 = System.nanoTime();
int[] a = { -1, 5, 9, 15, 85, 98, 100 };
int[] b = { -2, 6, 8, 14, 73, 85, 97 };
System.out.println(Arrays.toString(merge(a, b)));
System.out.println("done to take :" + ( System.nanoTime() - t1));
long t2 = System.nanoTime();
System.out.println(Arrays.toString(merge1(a, b)));
System.out.println("done to take :" + ( System.nanoTime() - t2));
}
}
package interview;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/*
* 控制台读入
方法一:使用new BufferedReader(new InputStreamReader(System.in))
方法二:使用java.util.Scanner类 (JDK1.5引入)
方法二:使用 java.io.Console类 (JDK1.6引入)
*/
public class ReadFromConsole {
public void readFromBufferedReader() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("请输入 :>");
try {
while (!"Exit".equals(line = br.readLine())) {
System.out.println("输入的内容为 = " + line);
System.out.print("请输入 :> ");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("程序结束");
}
@SuppressWarnings("resource")
public void readFromScanner() {
Scanner scanner = new Scanner(System.in);
String line = null;
System.out.print("请输入 :> ");
while (!"exit".equals(line = scanner.nextLine())) {
System.out.println("输入的内容为 = " + line);
System.out.print("请输入 :> ");
}
System.out.println("程序结束");
}
public void readFromConsole() {
Console console = System.console();
if (null == console) {
System.out.println("Console为null,不可用");
System.exit(0);
}
String line = null;
System.out.print("请输入 :> ");
while (!"Exit".equals(line = console.readLine())) {
System.out.println("输入的内容为 = " + line);
System.out.print("请输入 :> ");
}
System.out.println("程序结束");
}
public static void main(String[] args) {
ReadFromConsole test = new ReadFromConsole();
test.readFromBufferedReader();
test.readFromScanner();
test.readFromConsole();
}
}
package interview;
public class CharacterStatictics {
public static void main(String[] args) {
int chineseCharCount = 0;
int spaceCount = 0;
int digitCount = 0;
int lettersCount = 0;
int otherChars = 0;
String value = "Hello world! Welcome to Java world! 1234567890 Java 字符统计个数小程序!";
char[] chars = value.toCharArray();
for (char c : chars) {
if (isChineseCharacter(c)) {
chineseCharCount++;
} else if (Character.isLetter(c)) {
lettersCount++;
} else if (Character.isDigit(c)) {
digitCount++;
} else if (' ' == c) {
spaceCount++;
} else {
otherChars++;
}
}
System.out.println("中文字符:" + chineseCharCount);
System.out.println("数字:" + digitCount);
System.out.println("字母:" + lettersCount);
System.out.println("空格:" + spaceCount);
System.out.println("其它字符:" + otherChars);
}
private static boolean isChineseCharacter(char c) {
return c >= '\u4E00' && c <= '\u9FBF';
}
}
package interview;
/**
* 两个线程陷入死锁
* @author xl
*
*/
public class DeadlockExample {
String resource1 = "资源1";
String resource2 = "资源2";
Thread t1 = new Thread("线程1") {
public void run() {
while (true) {
synchronized (resource1) {
synchronized (resource2) {
System.out.printf("线程1拥有[%s], 需要[%s]\n", resource1,
resource2);
}
}
}
}
};
Thread t2 = new Thread("线程2") {
public void run() {
while (true) {
synchronized (resource2) {
synchronized (resource1) {
System.out.printf("线程2拥有[%s], 需要[%s]\n", resource2,
resource1);
}
}
}
}
};
public static void main(String a[]) {
DeadlockExample test = new DeadlockExample();
test.t1.start();
test.t2.start();
}
}