今天面的一個創業公司,面試官非常Nice,技術大牛一個,可惜問我的問題我大多不會,又跪了,把面試題整理出來,大家討論下吧
1. 怎麼把兩個數組 int a[] = {1,5,9,3,6}; 和 int b[] = {2,7,4};合併成一個數組並排序。
int a[] = {1,5,9,3,6};
int b[] = {2,7,4};
int[] result = new int[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
Arrays.sort(result);
for (int i : result) {
System.out.println("Data:" + i);
}
2. 寫一個單利模式
package com.mianshi.test1;
public class SingleModel {
public static void main(String[] args) {
ConnectionPoolA ca = ConnectionPoolA.getConnectionPool();
ConnectionPoolA ca2 = ConnectionPoolA.getConnectionPool();
System.out.println(ca==ca2);
}
}
//惡漢式 單例。 優點:實現簡單;缺點:在不需要的時候,白創造了對象,造成資源浪費
class ConnectionPoolA{
private static ConnectionPoolA connectionPoolA = new ConnectionPoolA();
private ConnectionPoolA(){}
public static ConnectionPoolA getConnectionPool(){
return connectionPoolA;
}
}
//懶漢式 單列。優點:需要對象時候才創建;缺點:線程不安全
class ConnectionPoolB{
private static ConnectionPoolB connectionPoolB;
private ConnectionPoolB(){}
public static synchronized ConnectionPoolB getConnectionPool(){
if(connectionPoolB == null){
connectionPoolB = new ConnectionPoolB();
}
return connectionPoolB;
}
}