迅雷Java上机题

//假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring,replaceAll等)?  
public class XLTest {
	public static void main(String[] args) {
		String str1 = "6sabcsssfsfs33";
		String str2 = "ab3";
		String str = new XLTest().deleteChars(str1, str2);
		System.out.println(str);
	}
	
	String deleteChars(String str1,String str2) {
		char[] chars1 = str1.toCharArray();
		char[] chars2 = str2.toCharArray();
		int len1 = chars1.length;
		int len2 = chars2.length;
		boolean[] flag = new boolean[len1];
		for(int i=0;i<len1;i++){
			flag[i] = false;
		}
		for(int i=0;i<len2;i++) {
			for(int j=0;j<len1;j++){
				if(chars2[i]==chars1[j]){
					flag[j] = true;
				}
			}
		}
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<len1;i++){
			if(flag[i]==false){
				sb.append(chars1[i]);
			}
		}
		return sb.toString();
	}
}

重点在于高效,本人算是抛砖引玉了,欢迎轻拍

//有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…  
public class TestThread {
	public static int ii = 0;
	public static void main(String[] args) {
		Thread t1 = new Thread1("A");
		Thread t2 = new Thread2("B");
		Thread t3 = new Thread3("C");
		t1.start();
		t2.start();
		t3.start();
	}
	public static boolean isToQuit() {
		if (TestThread.ii >= 30)
			System.exit(0);
		return true;
	}
}

class Thread1 extends Thread {
	public Thread1(String s) {
		super(s);
	}

	public void run() {
		synchronized (TestThread.class) {
			while (TestThread.isToQuit()) {
				if (TestThread.ii % 3 == 0) {
					System.out.print(Thread.currentThread().getName());
					TestThread.ii++;
				}
				
				try {
					TestThread.class.wait();
				} catch (Exception e) {
				}
				TestThread.class.notifyAll();
			}
		}
	}
}

class Thread2 extends Thread {
	public Thread2(String s) {
		super(s);
	}

	public void run() {
		synchronized (TestThread.class) {
			while (TestThread.isToQuit()) {
				if (TestThread.ii % 3 == 1) {
					System.out.print(Thread.currentThread().getName());
					TestThread.ii++;
				}
				TestThread.class.notifyAll();
				try {
					TestThread.class.wait();
				} catch (Exception e) {
				}
			}
		}
	}
}

class Thread3 extends Thread {
	public Thread3(String s) {
		super(s);
	}

	public void run() {
		synchronized (TestThread.class) {
			while (TestThread.isToQuit()) {
				if (TestThread.ii % 3 == 2) {
					System.out.print(Thread.currentThread().getName());
					TestThread.ii++;
				}
				TestThread.class.notifyAll();
				try {
					TestThread.class.wait();
				} catch (Exception e) {
				}
				
			}
		}
	}
}


你可能感兴趣的:(java,thread,编程,exception,String,Class)