博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一些Java编程题
阅读量:4030 次
发布时间:2019-05-24

本文共 5856 字,大约阅读时间需要 19 分钟。

本文为学习实践的记录,来源于:

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();	}}

转载地址:http://srlbi.baihongyu.com/

你可能感兴趣的文章
欢迎使用CSDN-markdown编辑器
查看>>
Android计算器实现源码分析
查看>>
Android系统构架
查看>>
Android 跨应用程序访问窗口知识点总结
查看>>
各种排序算法的分析及java实现
查看>>
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
jtag dump内存数据
查看>>
linux和windows内存布局验证
查看>>
linux config
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>