1.以下代碼結果是什麼?
public class Test4 {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a, b);
System.out.println(a + "." + b);//AB.B
}
static void operate(StringBuffer x, StringBuffer y) {
//x和main中a引用了同一個在堆中的StringBuffer對象
//y和main中b引用了同一個在堆中的StringBuffer對象
x.append(y);//由於調用了append的作用,x堆中的對象被改變。由"A"變"AB"
//有因爲a和x指向同一個引用,所以a改爲“AB”
y = x;//y直指向了x所指向的對象,所以y爲“AB”
//但是b還是指向原來的堆,所以b的值爲“B”
System.out.println(x+"."+y);//AB.AB
}
}
2.下列哪個說法是正確的(D)
A.ConcurrentHashMap使用synchronized關鍵字保證線程安全
B.HashMap實現了Collction接口
C.Array.asList方法返回java.util.ArrayList對象
D.SimpleDateFormat是線程不安全的
解析:
A.ConcurrentHashMap使用的是Segement(繼承自 ReentrantLock )分段鎖的技術來保證同步的, 使用synchronized關鍵字保證線程安全的是HashTable
B.HashMap實現的是Map接口
C.Arrays.asList方法返回List列表
D.SimpleDateFormat查看Java源碼可以看到,它的方法都不是用Synchronized修飾的,也沒有采用其他的同步措施
3.以下說法錯誤的是(D)
A.虛擬機中沒有泛型,只有普通類和普通方法
B.所有泛型類的類型參數在編譯時都會被擦除
C.創建泛型對象時請指明類型,讓編譯器儘早的做參數檢查
D.泛型的類型擦除機制意味着不能在運行時動態獲取List中T的實際類型
解析:
D.可以通過反射得到T的真實類型
4.標題:神奇的口袋 (動態規劃)
有一個神奇的口袋,總的容積是40,用這個口袋可以變出一些物品,這些物品的總體積必須是40。John現在有n個想要得到的物品,每個物品的體積分別是a1,a2……an。John可以從這些物品中選擇一些,如果選出的物體的總體積是40,那麼利用這個神奇的口袋,John就可以得到這些物品。現在的問題是,John有多少種不同的選擇物品的方式。
public class Test9 {
public static int count(int[] array,int sum,int index){
//sum代表總容積爲40,index代表數組下標
if(sum==0){
return 1;
}
//當下標==長度時,證明沒有組合數
if(index==array.length){
return 0;
}
//計算出從index+1開始,包含array[index]的組合數
int a = count(array,sum-array[index],index+1);
//計算出從index+1開始,不包含array[index]的組合數
int b = count(array,sum,index+1);
return a+b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] array = new int[num];
for(int i = 0;i<num;i++){
array[i] = sc.nextInt();
}
int result = count(array,40,0);
System.out.println(result);
}
}
//方法二
public class Test5 {
static int[] weight;
static int N;
static int count = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
N = input.nextInt();
weight = new int[N + 1];
for (int i = 1; i <= N; i++) {
weight[i] = input.nextInt();
}
count(40, N);
System.out.println(count);
}
}
public static void count(int s, int n) {
//如果正好裝滿
if (s == 0) {
++count;
return;
}
//是s<0或n<1則不能完成
if (s < 0 || (s > 0 && n < 1))
return;
count(s - weight[n], n - 1);
count(s, n - 1);
}
}
- 標題:用兩個棧實現隊列
用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素爲int類型。
public class Test10 {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack2.size()==0){
while (!stack1.isEmpty()){
int num = stack1.pop();
stack2.push(num);
}
}
int num = 0;
if(stack2.size()!=0){
num = stack2.pop();
}else {
return -1;
}
return num;
}
}
//方法二
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack1.empty()&&stack2.empty()){
throw new RuntimeException("Queue is empty!");
}
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}