1、看输出()
public class Test {
public static void main(String sgf[]) {
StringBuffer a=new StringBuffer("A");
StringBuffer b=new StringBuffer("B");
operate(a,b);
System.out.println(a+"."+b);
}
static void operate(StringBuffer x,StringBuffer y) {
x.append(y);
y=x;
}
}
AB.B
2、在JAVA中,假设A有构造方法A(int a),则在类A的其他构造方法中调用该构造方法和语句格式应该为()
this(x)
3、下列哪个说法是正确的()
SimpleDateFormat是线程不安全的
4、以下说法错误的是()
A 虚拟机中没有泛型,只有普通类和普通方法
B 所有泛型类的类型参数在编译时都会被擦除
C 创建泛型对象时请指明类型,让编译器尽早的做参数检查
D 泛型的类型擦除机制意味着不能在运行时动态获取List中T的实际类型
5、看输出()
public class Example{
String str=new String("good");
char[]ch={'a','b','c'};
public static void main(String args[]){
Example ex=new Example();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and ");
System.out.print(ex.ch);
}
public void change(String str, char[] ch){
str="test ok";
ch[0]='g';
}
}
good and gbc
原因与第一题一样
6、神奇的口袋
思路:要n个物品,用一容量为n的数组record,表示每个物品的体积,i从0开始进行递归
public class Main {
static int[] record ;
static int n ;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
record =new int[n];
for (int i = 0; i < n; i++) {
record[i]=scanner.nextInt();
}
int count = fun(0, 40);
System.out.println(count);
}
static int fun(int i,int sum){
//结束
if (i==n) return 0;
//刚好够,注意,这里还可以不填充继续递归
if (record[i]==sum) return 1+fun(i+1,sum);
//没装满
else if (sum>record[i]) return fun(i+1,sum-record[i])+fun(i+1,sum);
return fun(i+1,sum);
}
}
7、两个栈实现队列
思路:把数字装进第一个栈,再从第一个栈取出装进第二个栈,从第二个栈取出的顺序就和队列一样
import java.util.Stack;
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();
}
}
加油鸭!