今天面的一个创业公司,面试官非常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;
}
}