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